mysql 导入数据 1215,mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决...

ERROR 1215 (HY000): Cannot add foreign key constraint

在用laravel建表时遇到了这个错误,然后找了下找到了解决办法,记录下:

是要建两张表:

Schema::create('users', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('name');

$table->string('email')->unique();

$table->timestamp('email_verified_at')->nullable();

$table->string('password');

$table->rememberToken();

$table->timestamps();

});

Schema::create('articles', function (Blueprint $table) {

$table->bigIncrements('id');

$table->unsignedTinyInteger('status');

$table->string('title');

$table->string('preview');

$table->longText('content');

$table->unsignedInteger('author_id')->nullable();

$table->timestamps();

$table->softDeletes();

$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');

$table->engine = 'InnoDB';

$table->charset = 'utf8mb4';

$table->collation = 'utf8mb4_unicode_ci';

});

出现如下提示:

700353865f11

微信截图_20191023204228.png

检查了数据库结构,如图

700353865f11

微信图片_20191023204630.png

700353865f11

微信图片_20191023204636.png

发现错误是users表主键使用了bigint 因为你要添加外键时,你的外键类型就是bigint,而引用的主键类型也必须是 bigint,如果你的主键设置auto_increment(那么你的字段类型实际上是unsigned_bigint)

得出结论:主键,外键类型不一致,所以无法添加外键。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值