【问题标题】:Laravel - Table migration crror with unsigned primary keyLaravel - 具有未签名主键的表迁移错误
【发布时间】:2016-07-02 11:50:02
【问题描述】:

目前我在 Laravel 中的迁移失败了:

public function up()
{
    Schema::create('site_permission_modules', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('site_permission_modules');
}

显示的错误如下:

SQLSTATE[HY000]:一般错误:1005 无法创建表 'orgasystem.site_permission_modules' (errno: 150) (SQL: create table site_permission_modules (id int unsigned not null auto_increment 主键,name var char(255) 不为空,created_at 时间戳 null, updated_at timestamp null) 默认字符集 utf8 collat​​e utf8_unicode_ci)

我有很多其他的表都成功了,没有任何问题。

当我将 Laravel 抛出的 SQL 语句复制到 MySQL 时,它也会失败,但只要我删除主键上的 unsigned 关键字,它就会成功。声明见下文。

失败:

create table `site_permission_modules` (
    `id` int unsigned not null auto_increment primary key, 
    `name` varchar(255) not null, 
    `created_at` timestamp null, 
    `updated_at` timestamp null) 
default character set utf8 collate utf8_unicode_ci;

成功:

create table `site_permission_modules` (
    `id` int not null auto_increment primary key, 
    `name` varchar(255) not null, 
    `created_at` timestamp null, 
    `updated_at` timestamp null) 
default character set utf8 collate utf8_unicode_ci;

有人知道为什么会这样吗?

【问题讨论】:

  • 你的php和mysql版本?
  • 使用了你的“失败”的 sql,它在我的 mysql 上运行良好。也许你应该检查你的 mysql 版本。
  • 您的“失败”SQL 看起来不错,并且在我的 MySQL(版本 5.6.26)上也可以工作。
  • @Riaan 您可以使用以下 sql 语句检查您的 mysql 版本:SHOW VARIABLES LIKE "%version%";,另外,看看您是否使用innoDBMyISAM 有一些细微的差异可能会产生这种情况问题
  • 好吧,可能是mysql版本,我目前正在使用mamp,我发现它还在5.5上。我会在家里检查它是否在我的新版本上运行并在这里确认。

标签: php mysql laravel laravel-5.2


【解决方案1】:

原来的答案是 MySQL 版本太低,而在 MySQL 5.5 版上运行 MAMP 时,迁移不起作用。我已经在 5.7 版本上测试了迁移,它成功运行了所有迁移。

但这确实让我有点困惑,因为我在使用相同版本的 MAMP 的同时完成了其他 Laravel 迁移。

这可能与我首先测试的机器上的 MySQL 默认引擎有关,遗憾的是我目前无法对其进行测试。

【讨论】:

    猜你喜欢
    • 2014-05-30
    • 2020-05-07
    • 2021-03-20
    • 1970-01-01
    • 2019-01-31
    • 2014-09-27
    • 2018-09-22
    • 2016-07-06
    • 2015-08-14
    相关资源
    最近更新 更多