您好我有一个使用迁移架构生成器创建表的问题。 问题出现在具有自引用外键的表中。 这里是其产生错误的代码:Laravel迁移自引用外键问题
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');
});
以下是错误:
SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150) (SQL: alter table `cb_cate
血污add constraint cb_category_parent_id_foreign foreign key (
上德尔 ETE级联PARENT_ID ) references
cb_category (
id`)上更新级联)(绑定:阵列( ))
[PDOException] SQLSTATE [HY000]:一般错误:1005无法创建表'eklik2。# sql-7d4_e'(errno:150)
任何想法?
我打破它在2个语句,无论如何错误依然存在: Schema :: create(...); ('cb_category') - > onUpdate('cascade)'('cb_category') - >参考('id') - >外部('parent_id') - ') - > onDelete('cascade'); }); – gandra404
解决打破。这里是代码: Schema :: create('cb_category',function($ table){...}); $ dbh = DB :: getPdo(); $ dbh-> query(“ALTER TABLE cb_category ADD CONSTRAINT fk_cb_category_parent_id FOREIGN KEY(parent_id)REFERENCES cb_category(id)ON DELETE NO ACTION ON UPDATE NO ACTION”); – gandra404