1

I created a new Migration script on CakePHP 3 using the following command

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

The id field should be the primary key, and topic_id is a foreign key. The script comes out like I want, except topic_id is a string for some reason, but I manually fix that.

When I attempt to run the script I get an error stating:

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'

I have other tables with a column name of 'id', but this is a new table, called with the create() function,

Brady Pacha
  • 313
  • 3
  • 15
  • 2
    https://book.cakephp.org/3.0/en/migrations.html#creating-custom-primary-keys – ndm Feb 27 '17 at 22:59
  • @ndm Ah, that was it. the migration script will automatically create the id and when I was trying to create one too, there was a duplicate column error. Thanks. – Brady Pacha Feb 28 '17 at 16:27

3 Answers3

0

Remove ID. ID column is automatically created, so you don't need to write it.
bin/cake bake migration CreateOfficialTeams name:string topic_id:int

lechat
  • 390
  • 2
  • 15
0

The primary key column named id will be added implicitly. CakePHpp 3 For more detail you can visit on Cakephp Migration overview

Vipul
  • 896
  • 7
  • 14
0

Unusual behavior.

In the cakeBook: "The primary key column named id will be added implicitly." but in bilder adds to the migration files:

bin/cake bake migration_spanshot Initial

Result file:

 $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,
            ])
PLI52KA
  • 11
  • 3