[英]General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”)
[英]Laravel General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed")
尝试在 Laravel 中迁移并弹出此错误。 我认为错误可能是 bigInteger 类型,因为我在另一个相同的问题答案中发现。 请参阅将 bigInteger 更改为 bigIncrements,但我没有遇到有关类型约束的另一个错误。
代码
public function up()
{
Schema::create('categories_products', function (Blueprint $table) {
$table->bigInteger('category_id')->unsigned()->index();
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
});
}
错误
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `categori
es_products` add constraint `categories_products_category_id_foreign` foreign key (`category_id`) references `categories` (`id`) on delete cascade)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed")
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed")
谢谢。
如果没有看到关系的目标表上的列定义,很难给出明确的答案。 类型需要匹配。 因此,如果您在 id 字段中使用 bigIncrements(),那么unsignedBigInteger()
或bigInteger()->unsigned()
都应该用作外键的类型,但是如果您使用increments
作为 id 则外键需要是unsignedInteger()
或integer()->unsigned()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.