Phinx: Setting primary_key to false results in SQL error - not what is documented

Created on 13 Sep 2016  路  2Comments  路  Source: cakephp/phinx

Somewhat related to #28:

http://docs.phinx.org/en/latest/migrations.html#creating-a-table

The documentation explicitly says this for disabling auto-PK behaviour:

The id option sets the name of the automatically created identity field, while the primary_key option selects the field or fields used for primary key. The primary_key option always defaults to the value of id. Both can be disabled by setting them to false.

(Emphasis mine)

For example, this migration:

 $table = $this->table('foo_bar', ['id' => false, 'primary_key' => false]);
        $table
            ->addColumn('foo_id', 'integer')
            ->addColumn('bar_id', 'integer')
            ->create();

Will produce the following SQL:

CREATE TABLE `foo_bar` (`foo_id` INT(11) NOT NULL, `bar_id` INT(11) NOT NULL, PRIMARY KEY ()) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci

Which obviously gives the following vague SQL error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci' at line 1

The above migration can be altered to run just fine:

 $table = $this->table('foo_bar', ['id' => false]);
        $table
            ->addColumn('foo_id', 'integer')
            ->addColumn('bar_id', 'integer')
            ->create();

Thus:

  1. Either remove the last line in the documentation (setting id to false is enough to not get the auto-PK behaviour)
  2. Or alter the if statement if (isset($options['primary_key'])) { to also make sure it's non-boolean. (and thus the documentation matches the behaviour)
bug docs

Most helpful comment

Hi @AshleyPinner

This feature works

$table = $this->table('followers', ['id' => false, 'primary_key' => false ]); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime') ->create();

will produce

== 20200415183123 CreateMyNewMigration: migrating START TRANSACTION CREATE TABLEfollowers(user_idINT(11) NOT NULL,follower_idINT(11) NOT NULL,createdDATETIME NOT NULL, PRIMARY KEY ()) ENGINE = InnoDB CHARACTER SET latin1 COLLATE latin1_swedish_ci; COMMIT INSERT INTOphinxlog(version,migration_name,start_time,end_time,breakpoint) VALUES ('20200415183123', 'CreateMyNewMigration', '2020-04-15 18:38:44', '2020-04-15 18:38:44', 0); == 20200415183123 CreateMyNewMigration: migrated 0.0047s

Correct:
$table = $this->table('followers', ['id' => false ]); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime') ->create();

produces
== 20200415183123 CreateMyNewMigration: migrating START TRANSACTION CREATE TABLEfollowers(user_idINT(11) NOT NULL,follower_idINT(11) NOT NULL,createdDATETIME NOT NULL) ENGINE = InnoDB CHARACTER SET latin1 COLLATE latin1_swedish_ci; COMMIT INSERT INTOphinxlog(version,migration_name,start_time,end_time,breakpoint) VALUES ('20200415183123', 'CreateMyNewMigration', '2020-04-15 18:35:21', '2020-04-15 18:35:21', 0); == 20200415183123 CreateMyNewMigration: migrated 0.0056s

This can be closed

All 2 comments

A docs label should be added to this.

Hi @AshleyPinner

This feature works

$table = $this->table('followers', ['id' => false, 'primary_key' => false ]); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime') ->create();

will produce

== 20200415183123 CreateMyNewMigration: migrating START TRANSACTION CREATE TABLEfollowers(user_idINT(11) NOT NULL,follower_idINT(11) NOT NULL,createdDATETIME NOT NULL, PRIMARY KEY ()) ENGINE = InnoDB CHARACTER SET latin1 COLLATE latin1_swedish_ci; COMMIT INSERT INTOphinxlog(version,migration_name,start_time,end_time,breakpoint) VALUES ('20200415183123', 'CreateMyNewMigration', '2020-04-15 18:38:44', '2020-04-15 18:38:44', 0); == 20200415183123 CreateMyNewMigration: migrated 0.0047s

Correct:
$table = $this->table('followers', ['id' => false ]); $table->addColumn('user_id', 'integer') ->addColumn('follower_id', 'integer') ->addColumn('created', 'datetime') ->create();

produces
== 20200415183123 CreateMyNewMigration: migrating START TRANSACTION CREATE TABLEfollowers(user_idINT(11) NOT NULL,follower_idINT(11) NOT NULL,createdDATETIME NOT NULL) ENGINE = InnoDB CHARACTER SET latin1 COLLATE latin1_swedish_ci; COMMIT INSERT INTOphinxlog(version,migration_name,start_time,end_time,breakpoint) VALUES ('20200415183123', 'CreateMyNewMigration', '2020-04-15 18:35:21', '2020-04-15 18:35:21', 0); == 20200415183123 CreateMyNewMigration: migrated 0.0056s

This can be closed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alex-barylski picture alex-barylski  路  14Comments

ricksanchez picture ricksanchez  路  15Comments

igorsantos07 picture igorsantos07  路  30Comments

hmp5bs picture hmp5bs  路  17Comments

JamesTheHacker picture JamesTheHacker  路  15Comments