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:
if (isset($options['primary_key'])) { to also make sure it's non-boolean. (and thus the documentation matches the behaviour)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
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.0047sCorrect:
$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.0056sThis can be closed