🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۲ امیر ابوئی
علت ارور هنگام migrateکردن جداول!
مهرداد سامی حل شده توسط مهرداد سامی

سلام استاد، وقتتون بخیر

ببخشید یه سوال داشتم من کدهامو مشابه شما توی ویدیو زدم ولی وقتی migrate کردم به ارور زیر برخوردم:

  SQLSTATE[HY000]: General error: 1005 Can't create table `shopDB`.`order_product` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `order_product` add constraint `order_product_order_id_foreign` foreign key (`order_id`) references `orders` (`id`) on delete cascade)

علاوه بر اینکه دوتا فیلد phone و address هم به جدول users اضافه نشدن، مشکل از چی هستش؟ 

کدهای مربوط به ادکردن فیلدها به جدول user + کد جدول order_prodcut رو هم براتون گذاشتم، ممنون میشم بررسی کنید بگید بهم کجا احیاناً اشتباهی رخ داده!

class AddPhoneNumberAndAddressIntoUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->char('phone_number')->nullable();
            $table->string('address')->nullable();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            //
        });
    }
}

 

public function up()
    {
        Schema::create('order_product', function (Blueprint $table) {
            $table->bigInteger('order_id');
            $table->bigInteger('product_id');
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->timestamps();
        });
    }

سلام 

 

احتمالا type فیلد id در جدول product و جدول order با این جدول شما یکی نیست. 

 

شما اینجا bigInteger تعریف کردید .. داخل جداول product و order هم فیلد id به همین صورت تعریف شده؟

بهترین پاسخ
مهرداد سامی ۰۱ تیر ۱۴۰۰، ۱۷:۲۲

متاسفانه حواسم نبود و در migration جدول users، مقدار id به صورت id ست شده بود ن bigIncrements که درستش کردم و حل شد

ممنون

امیر ابوئی ۰۲ تیر ۱۴۰۰، ۰۵:۱۶