【问题标题】:Laravel 4 Migration error - creates two auto_increment primary keys fieldsLaravel 4 迁移错误 - 创建两个 auto_increment 主键字段
【发布时间】:2013-07-26 10:28:32
【问题描述】:

我使用此设置进行了迁移:

$table->increments('id');
$table->integer('user_id', 10)->unsigned(); // this is meant to be used as a foreign key

执行 php artisan migrate 后返回错误:

[Exception]                                                                                                                                                                                 
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition;
there can be only one auto column and it must be defined as a key (SQL: create table `transactions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null auto_increment primary key) default character set utf8 collate utf8_unicode_ci) (Bindings: array ())

我没有将 user_id 指定为 auto_increment 主键,但 Migration 将其视为这样。

如何在 Migrations 中创建外键?

【问题讨论】:

    标签: laravel laravel-4 database-migration


    【解决方案1】:

    在 Laravel 4 中,整数函数中的第二个参数用于指示整数列是否自动递增(因此主键与否) 就我而言,要在表中添加自动递增的 id,我会这样写

    $table->integer('id' , true);
    

    它创建一个 11 位的整数列。

    【讨论】:

      【解决方案2】:

      为什么不将 user_id 指定为具有自动增量功能的主键?

      $table->increments('user_id');
      // other columns
      ...
      

      架构生成器创建 user_id,它是 10 位长、无符号和主键。

      【讨论】:

        【解决方案3】:

        @crynobone:第二个参数用于布尔值确定主键,整数没有长度选项。

        参考这里:https://github.com/laravel/laravel/issues/2212#issuecomment-21608193

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2012-12-21
        • 1970-01-01
        • 2014-06-10
        • 2014-05-30
        • 2021-03-20
        • 1970-01-01
        相关资源
        最近更新 更多