محسن
5 سال پیش توسط محسن مطرح شد
5 پاسخ

مرتبط داده نشدن بین جداول

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

QueryException  : SQLSTATE[HY۰۰۰]: General error: ۱۰۰۵ Can't create table `project`.`#sql-a۲c_f۹` (errno: ۱۵۰ "Foreign key constraint is incorrectly formed") (SQL: alt
 constraint `articles_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

sktop\laravel۲\vendor\laravel\framework\src\Illuminate\Database\Connection.php:۶۶۴
an exception occurs when attempting to run a query, we'll format the error
sage to include the bindings with SQL, which will make this exception a
 more helpful to the developer instead of just the database's errors.
(Exception $e) {
row new QueryException(
  $query, $this->prepareBindings($bindings), $e

وقتی کد php artisan migrate رو میزنم این ارور میده.

    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('title');
            $table->string('slug');
            $table->text('description');
            $table->text('body');
            $table->string('imageUrl');
            $table->string('tags');
            $table->integer('commentCount')->default(۰);
            $table->integer('viewCount')->default(۰);
            $table->timestamps();
        });

ثبت پرسش جدید
Alimotreb
تخصص : کانفیگ سرور و برنامه نویس
@Alimotreb 5 سال پیش مطرح شد
0

سلام
@mohsenmlm76

این خط رو پاک کنید از مایگریشن


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

زیر همین schema

Schema::table('articles', function($table)
{
              $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

});

این رو قرار بدید

از ورژن چند mysql داریداستفاده میکنید؟


محسن
@mohsenmlm76 5 سال پیش آپدیت شد
0

نه درست نشد.متاسفانه
از ورژن 7.3.3 استفاده میکنم.اخرین ورژن xamp


mahdi khanzadi
تخصص : backend developer at Snapp mar...
@khanzadimahdi 5 سال پیش مطرح شد
0

خطی که برای ساخت کلید خارجی استفاده میشه رو در انتها قرار بدید سپس دوباره ران کنید کد رو.


محسن
@mohsenmlm76 5 سال پیش مطرح شد
0

در انتها قرار دادم ولی هنوز درست نشد.


محسن
@mohsenmlm76 5 سال پیش آپدیت شد
0

دوستان حل شد ممنون از کمکتون در جدول users این کد روتغییر دادم ولی دلیلش رو متوجه نمیشم چرا تغییر دادم دیگ ارور نداد؟

 $table->bigIncrements('id');

bigincrement بود به increment تبدیل کردم


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

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