mysql - 添加外键 SQL 违规,Laravel

标签 mysql database laravel foreign-keys

我需要你的帮助! :( 我正在尝试在带有数据的现有表中添加外键,但我收到一个错误,我认为是因为该表有数据,我测试了新数据库中的所有迁移并且一切正常,有人知道是否可以添加外键是一个有数据的表?

Schema::table('cicles', function(Blueprint $table) {

        $table->integer('shiftId')->unsigned()->change();
        $table->foreign('shiftId')->references('id')->on('shifts');

    });

当刚刚运行迁移来添加外键时 This when just run the migration to add the foreign key

这是运行所有迁移的时间

enter image description here

最佳答案

如果现有数据违反了新约束,则无法添加外键约束。要查找违反约束的数据,请执行外连接。

SELECT circles.*
FROM circles
     LEFT OUTER JOIN shifts
       ON circles.shiftId = shifts.id
WHERE shifts.id IS NULL

关于mysql - 添加外键 SQL 违规,Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54890999/

相关文章:

php - MySql 选择引号

php - 插入和搜索多个关键字

php - 如何修复 "Warning: mysqli::__construct(): (HY000/1045): Access denied for user ' 用户的 @'localhost'(使用密码 : YES)"on MacOS

c - 用C模拟数据库

php - 获取给定里程的特定区域的数据

laravel - Laravel 4 教程 - 身份验证

java - Java 中的 MYSQL 脚本

php - Zf2 Db Sessions 插入但不更新数据库中的 session

database - 如何在 postgres 中设置同步流复制?

php - 从数组打印值有时不起作用