SQLSTATE [23000]:在Laravel 4中迁移期间完整性约束违规

时间:2014-12-12 14:48:33

标签: mysql laravel-4 migration

我在迁移时遇到问题

Migration File

<?php

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

class AlterTableMm extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create('downloads', function($table){

            $table->engine = 'InnoDB';

            $table->increments('id')->unsigned();
            $table->string('title',255);
            $table->string('count',45)->default(0);
            $table->timestamps();
            $table->integer('user_id')->unsigned();


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

        });


        Schema::table('marketing_materials', function($table)
        {

            $table->string('download_total', 45 )->default(0);
            $table->integer('download_id')->unsigned();

            $table->foreign('download_id')->references('id')->on('downloads');


        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::table('users', function($table)
        {
            $table->dropForeign('users_user_id_foreign');

        });

        Schema::drop('downloads');


        Schema::table('marketing_materials', function($table)
        {
            $table->dropForeign('marketing_materials_download_id_foreign');

        });

        Schema::table('marketing_materials', function($table)
        {

            $table->dropColumn('download_total');
            $table->dropColumn('download_id');


        });
    }

}

Error Message

 [Illuminate\Database\QueryException]
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`distributor-local`.`#sql-668_3d`, CONSTRAINT `marketing_mater
  ials_download_id_foreign` FOREIGN KEY (`download_id`) REFERENCES `downloads` (`id`)) (SQL: alter table `marketing_materials` add constraint marketing_materials_download_id_foreign fo
  reign key (`download_id`) references `downloads` (`id`))


  [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`distributor-local`.`#sql-668_3d`, CONSTRAINT `marketing_mater
  ials_download_id_foreign` FOREIGN KEY (`download_id`) REFERENCES `downloads` (`id`))

我有所有必需的表格

  • 用户
  • marketing_materials
  • 我最近刚迁移了我的下载表

有人可以告诉我,我做错了什么吗?

让我误解了大部分错误信息的部分是,所有内容都被添加到数据库中。见下图

enter image description here

我也改变了我的marketing_materials表,这也是有效的。

enter image description here

我应该忽略错误信息???

0 个答案:

没有答案