【问题标题】:CakePHP Migration Creating a new table causes duplicate column nameCakePHP 迁移创建新表导致列名重复
【发布时间】:2017-02-27 22:10:17
【问题描述】:

我使用以下命令在 CakePHP 3 上创建了一个新的迁移脚本

bin/cake bake migration CreateOfficialTeams id:int name:string topic_id:int

id 字段应该是主键,topic_id 是外键。脚本按照我的意愿出现,除了 topic_id 出于某种原因是一个字符串,但我手动修复了它。

当我尝试运行脚本时,我收到一条错误消息:

Exception: SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id' in [/home/bradygp/workspace/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php, line 306]
2017-02-27 21:52:16 Error [PDOException] SQLSTATE[45S21]: Column already exists: 1060 Duplicate column name 'id'

我还有其他列名为“id”的表,但这是一个新表,使用create() 函数调用,

【问题讨论】:

  • @ndm 啊,就是这样。迁移脚本将自动创建 id,当我也尝试创建一个时,出现重复列错误。谢谢。

标签: php mysql cakephp migration phinx


【解决方案1】:

删除 ID。 ID 列是自动创建的,因此您无需编写它。
bin/cake bake 迁移 CreateOfficialTeams name:string topic_id:int

【讨论】:

    【解决方案2】:

    名为 id 的主键列将被隐式添加。 CakePHP 3 有关更多详细信息,您可以访问 Cakephp Migration overview

    【讨论】:

      【解决方案3】:

      异常行为。

      在 cakeBook 中: “将隐式添加名为 id 的主键列。” 但在 bilder 中添加了迁移文件:

      bin/cake bake migration_spanshot Initial
      

      结果文件:

       $this->table('admin_menus')
                  ->addColumn('id', 'integer', [
                      'autoIncrement' => true,
                      'default' => null,
                      'limit' => 11,
                      'null' => false,
                  ])
                  ->addPrimaryKey(['id'])
                  ->addColumn('role_user_id', 'integer', [
                      'default' => '0',
                      'limit' => 11,
                      'null' => true,
                  ])
      

      【讨论】:

        猜你喜欢
        • 2022-10-19
        • 2020-07-20
        • 2020-12-10
        • 2015-06-08
        • 2017-12-18
        • 2020-03-29
        • 1970-01-01
        • 2011-08-26
        • 2015-03-06
        相关资源
        最近更新 更多