quot;General error: 1005 Can#39;t create tablequot; Using Laravel Schema Build and Foreign Keys(“一般错误:1005 无法创建表使用 Laravel Schema 构建和外键)
问题描述
基本上,我和这个人有同样的问题,减去表前缀.因为我没有表前缀,所以他的修复不起作用.http://forums.laravel.com/viewtopic.php?id=972
Essentially, I am having the same issue as this guy, minus the table prefix. Because I have no table prefix, his fix does not work. http://forums.laravel.com/viewtopic.php?id=972
我正在尝试使用 Laravel 的 Schema Builder 构建一个表,如下所示:
I am trying to build a table using Laravel's Schema Builder like this:
Schema::create('lessons', function($table)
{
$table->increments('id');
$table->string('title')->nullable();
$table->string('summary')->nullable();
$table->timestamps();
});
Schema::create('tutorials', function($table)
{
$table->increments('id');
$table->integer('author');
$table->integer('lesson');
$table->string('title')->nullable();
$table->string('summary')->nullable();
$table->string('tagline')->nullable();
$table->text('content')->nullable();
$table->text('attachments')->nullable();
$table->timestamps();
});
Schema::table('tutorials', function($table)
{
$table->foreign('author')->references('id')->on('users');
$table->foreign('lesson')->references('id')->on('lessons');
});
问题是,当我运行此代码时(在/setup 路由中),我收到以下错误:
The issue is, when I run this code (in a /setup route), I get the following error:
SQLSTATE[HY000]: General error: 1005 Can't create table 'tutorials.#sql-2cff_da' (errno: 150)
SQL: ALTER TABLE `tutorials` ADD CONSTRAINT tutorials_author_foreign FOREIGN KEY (`author`) REFERENCES `users` (`id`)
Bindings: array (
)
基于网络上的帖子和关于如何设置 Laravel 的 Eloquent 关系的有限文档,我不确定我做错了什么......
Based on posts around the web and the limited documentation available on how to setup Laravel's Eloquent relationships, I'm not sure what I'm doing wrong...
users
已经存在,并且它确实有一个 id
字段,即 auto_increment
.我还在用正确的关系(belongs_to
和 has_many
)设置我的模型,但据我所知,这不是问题——这是数据库设置.DB 是 InnoDB.
users
already exists and it does have an id
field that is auto_increment
. I am also setting up my models with the proper relationships (belongs_to
and has_many
), but as far as I can tell this is not the issue-- it's the database setup. The DB is InnoDB.
外键到底做错了什么?
推荐答案
我不确定这些是否是失败的原因,但有几个指针.如果您使用旧版本的 mySQL 作为数据库,则默认表实现是不支持外键限制的 myISAM.由于您的脚本在外键分配上失败,您最好在 Schema 的 create 方法中使用此语法明确声明您希望 INNODB 作为引擎.
I'm not 100% sure if these are the reasons this is failing but a couple of pointers. If you're using an older version of mySQL as the database, the default table implementation is myISAM that does not support foreign key restraints. As your scripts are failing on the foreign key assignment, you are better off explicitly stating that you want INNODB as the engine using this syntax in Schema's create method.
Schema::create('lessons', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('title')->nullable();
$table->string('summary')->nullable();
$table->timestamps();
});
这有望缓解您遇到的问题.
This should hopefully alleviate the problems you are having.
此外,虽然您可以事后声明外键,但我会在初始架构中创建外键,因为我可以轻松检查以确保我拥有正确的数据库引擎集.
Also, whilst you can declare foreign keys as an afterthought, I create the foreign keys within the initial schema as I can do an easy check to make sure I've got the right DB engine set.
Schema::create('tutorials', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('author');
$table->integer('lesson');
$table->string('title')->nullable();
$table->string('summary')->nullable();
$table->string('tagline')->nullable();
$table->text('content')->nullable();
$table->text('attachments')->nullable();
$table->timestamps();
$table->foreign('author')->references('id')->on('users');
$table->foreign('lesson')->references('id')->on('lessons');
});
希望这有助于/解决您的问题.
Hope this helps / solves your problem.
这篇关于“一般错误:1005 无法创建表"使用 Laravel Schema 构建和外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“一般错误:1005 无法创建表"使用 Laravel Schema 构建和外键
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01