راکت
حل شده

مشکل با foreign در migrate کردن

4 پاسخ

سلام من خطا دارم زمان migrate کردن و اشاره می کنه به foreign که نمی دونم باید چی کار کنم؟
ورژن لاراول ۷
سیستم عامل لینوکس مانجارو
IDE phpstorm

مراحلی که پیش رفتم.

 php artisan make:model Account -m
 php artisan make:model Domain -m

کدهای فایلم:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAccountsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('accounts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('domain_id');
            $table->string('username', 10)->unique();
            $table->string('password', 255);
            $table->char('active', 1)->default('0');
            $table->string('mobile_verification', 10)->nullable()->default('');
            $table->string('email_verification', 100)->nullable()->default('');
            $table->string('remember_token', 255)->nullable()->default('');
            $table->string('api_token', 255)->nullable()->default('');
            $table->foreign('domain_id')->references('id')
                ->on('domains')
                ->onDelete('cascade');

            $table->foreign('user_id')->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('accounts');
    }
}

بعدیش:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDomainsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('domains', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->char('active', 1)->default('0');
            $table->integer('expire')->unsigned();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('domains');
    }
}

خب حالا model ها:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Domain extends Model
{
    //
    protected $table='domains';
}

و

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Account extends Model
{
    //
    protected $table='accounts';
}

دوستان نه فقط به این foreign بلکه به همه خطا میده به جز foreign برای جدول users

و بعد

php artisan migrate:fresh

و خطایی که میده:

Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.02 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.02 seconds)
Migrating: 2020_06_25_133135_create_accounts_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1005 Can't create table `iranftc`.`accounts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `accounts` add constraint `accounts_domain_id_foreign` foreign key (`domain_id`) references `domains` (`id`) on delete cascade)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

      +9 vendor frames 
  10  database/migrations/2020_06_25_133135_create_accounts_table.php:41
      Illuminate\Support\Facades\Facade::__callStatic()

      +34 vendor frames 
  45  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
ثبت پرسش جدید

پاسخ‌ها

4
سید سعید حسینی
سید سعید حسینی
@saeedhosseini

تخصص: برنامه نویس

بهترین پاسخ

سلام
شما دارید کلید خارجی رو به جدول domains متصل میکنید درحالی که این جدول هنوز ساخته نشده. پس اول باید migration مربوط به domains اجرا بشه و بعد accounts. برای اینکار میتونی اعداد تو اسم فایل (تایم ساخت) رو طوری تغییر بدی که از نظر زمانی جلوتر بیفته و اول اجرا بشه.

کیارش مالکی
کیارش مالکی
@nader.register

تخصص: Backend developer

آقا ممنونم ازتون مشکل همینی بود که گفتید. تشکر

سید سعید حسینی
سید سعید حسینی
@saeedhosseini

تخصص: برنامه نویس

خواهش میکنم
موفق باشید

شاید پاسخ شما همین‌جا باشد

حسین شیری نژاد
حسین شیری نژاد
@hosseinshirinegad98

تخصص: programmer

بله ترتیب اجرای این مایگریت ها مهم هست
@nader.register

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام