Laravel migration self referencing foreign key issue(Laravel 迁移自引用外键问题)
问题描述
您好,我在使用迁移架构生成器创建表时遇到问题.具有自引用外键的表出现问题.这是产生错误的代码:
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');
});
这是错误:
SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150) (SQL: alter table `cb_cate
gory在 del 上添加约束 cb_category_parent_id_foreign 外键 (parent_id) 引用cb_category(id`)更新级联上的级联)(绑定:数组())
goryadd constraint cb_category_parent_id_foreign foreign key (parent_id) referencescb_category(id`) on del
ete cascade on update cascade) (Bindings: array (
))
[PDO异常]SQLSTATE[HY000]:一般错误:1005 无法创建表 'eklik2.#sql-7d4_e' (errno: 150)
[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150)
有什么想法吗?
推荐答案
你必须把它分成两个 Schema 块,一个创建列,另一个添加 FK.mysql不能同时做这两件事.
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.
这篇关于Laravel 迁移自引用外键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 迁移自引用外键问题
基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
