Laravel 迁移自引用外键问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18427391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel migration self referencing foreign key issue
提问by gandra404
Hi I have a problem to create a table using migration schema builder. The problem occure with table with self referencing foreign key. Here is the code which produce error:
嗨,我在使用迁移模式构建器创建表时遇到问题。带有自引用外键的表出现问题。这是产生错误的代码:
Schema::create('cb_category', function($table)
{
$table->integer('id')->primary()->unique()->unsigned();
$table->integer('domain_id')->unsigned();
$table->foreign('domain_id')->references('id')->on('cb_domain');
$table->integer('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade');
$table->string('name');
$table->integer('level');
});
Here is the error:
这是错误:
SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150) (SQL: alter table `cb_cate
goryadd constraint cb_category_parent_id_foreign foreign key (
parent_id) references
cb_category(
id`) on del
ete cascade on update cascade) (Bindings: array (
))
血腥add constraint cb_category_parent_id_foreign foreign key (
parent_id ) references
cb_category (
id`) 在删除级联上更新级联)(绑定:数组())
[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150)
[PDOException] SQLSTATE[HY000]:一般错误:1005 无法创建表 'eklik2.#sql-7d4_e' (errno: 150)
Any idea?
任何的想法?
采纳答案by Meroje
You have to break this into two Schema blocks, one creating the columns, the other adding the FKs. mysql can't do both at the same time.
您必须将其分解为两个 Schema 块,一个创建列,另一个添加 FK。mysql 不能同时进行。
回答by ioni
I may be too late for the party, but the official docs claim that the foreign key, in case of integer, must be ->unsigned();
我可能来晚了,但官方文档声称外键,在整数的情况下,必须是 ->unsigned();
http://laravel.com/docs/4.2/schema#foreign-keys
http://laravel.com/docs/4.2/schema#foreign-keys
Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.
注意:创建引用递增整数的外键时,请记住始终使外键列无符号。
Also, Artisan does not fail if you (as I have) misspell unsigned()
and I have spent quite a few hours trying to figure out why the key was not created.
此外,如果您(就像我一样)拼写错误,Artisan 也不会失败,unsigned()
而且我花了好几个小时试图找出未创建密钥的原因。
So two things:
1. Always make the foreign key column unsigned in case of incrementing integers
2. Check the spelling of unsigned()
所以有两件事:1. 在递增整数的情况下始终使外键列无符号 2. 检查拼写 unsigned()
回答by Isaac Limón
Two querys work :
两个查询工作:
Schema::create('cb_category', function($table)
{
$table->integer('id')->primary()->unique()->unsigned();
$table->integer('parent_id')->nullable();
});
Schema::table('cb_category', function (Blueprint $table)
{
$table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade');
});
回答by ??????? ????
I think you have another table that referencesthe current table that you want to create. I had this problem and remove that table and my problem was solved
我认为您有另一个表引用了您要创建的当前表。我遇到了这个问题并删除了那个表,我的问题就解决了
回答by krunal nerikar
Schema::create('cb_category', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('domain_id')->unsigned();
$table->foreign('domain_id')->references('id')->on('cb_domain');
$table->integer('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade');
$table->string('name');
$table->integer('level');
});
Try this
尝试这个