我无法解决这个问题,我有这个创建语句:
create table `roles`
(
`name` varchar(255) not null,
`description` varchar(255) not null,
`created_at` timestamp default 0 not null,
`updated_at` timestamp default 0 not null
) default character set utf8 collate utf8_unicode_ci;
但是我收到错误:
外键约束格式不正确
有人可以建议我在这里做错了什么吗?
仅供引用,我正在使用 MySQL。
实际语句是使用 Laravel 迁移运行的,会出现错误:
public function up()
{
Schema::create('roles', function(Blueprint $table)
{
$table->string('name')->unique()->primary();
$table->string('description');
$table->timestamps();
});
}
最佳答案
我快速浏览了一下,发现了这个问题,它很好地解释了为什么会发生此错误:mysql Foreign key constraint is incorrectly formed error
这对您来说意味着什么?这意味着您在某处有一个外键字段,希望使用角色表中的主键填充,但两者之间的类型不同。我可以想象,在另一个表中的某个地方,您有一个列,例如 role_id
,它是 int、bigint、tinyint 等。无论哪种方式,您都不应该使用字符串作为主键。
关于php - MySQL错误 "Foreign key constraint is incorrectly formed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34262780/