Mysql 无法在现有表上添加外键

Mysql 无法在现有表上添加外键,mysql,laravel,laravel-4,foreign-keys,database-migration,Mysql,Laravel,Laravel 4,Foreign Keys,Database Migration,在初始化表时,我在另一个表中创建了完全相同的FK,具有相同的关系。但是这个不行。我得到以下错误: public function up() { Schema::table('partial_trips', function(Blueprint $table) { $table->unsignedInteger('driver_user_id')->after('main_trip_id'); });

在初始化表时,我在另一个表中创建了完全相同的FK,具有相同的关系。但是这个不行。我得到以下错误:

public function up()
    {
        Schema::table('partial_trips', function(Blueprint $table)
        {
            $table->unsignedInteger('driver_user_id')->after('main_trip_id');
        });

        Schema::table('partial_trips', function(Blueprint $table)
        {
            $table->foreign('driver_user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    }

查看其他帖子,关于这个问题,我只能找到列不
未签名
或者关系中的一列不存在的情况。在这里,我已经讨论了这两种情况,不知道问题可能是什么…

表中有一些数据意味着约束将失败,因为添加的列将被现有行设置为NULL。 在迁移中,默认情况下,每个列定义都不可为空。您必须指定nullable以允许外键为null值

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or u  
  pdate a child row: a foreign key constraint fails (`bpr`.`#sql-7c82_5  
  7d`, CONSTRAINT `partial_trips_driver_user_id_foreign` FOREIGN KEY (`  
  driver_user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE)

表格里有数据吗?如果表中有孤立记录,则无法添加FK。是的,引用表和被引用表都有数据。您必须先修复孤立记录,然后再修复。
$table->integer('driver_user_id')->unsigned()->nullable()->after('main_trip_id');