-
Notifications
You must be signed in to change notification settings - Fork 631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error in migration when adding a foreign key with reference option = RESTRICT #556
Comments
Fixed in the |
It seems that removing self::$_connection->query('SET FOREIGN_KEY_CHECKS=0'); crashes migrations. For the first table created, we need a foreign key to the table that has not yet been created. And we get an error:
Migrations, which previously worked well now fall with an error. |
// cc: @sergeysviridenko |
@topotru Could you provide more information and steps for reproduce? |
@sergeysviridenko CREATE TABLE `notes` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`text` TEXT NOT NULL,
`created_by` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `FK_notes_users` (`created_by`),
CONSTRAINT `FK_notes_users` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`)
)
COLLATE='utf8_general_ci' ENGINE=InnoDB;
CREATE TABLE `users` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci' ENGINE=InnoDB; And their migrations: class NotesMigration_100 extends Migration
{
public function morph()
{
$this->morphTable('notes', [
'columns' => [
new Column(
'id',
[
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'autoIncrement' => true,
'size' => 10,
'first' => true
]
),
new Column(
'text',
[
'type' => Column::TYPE_TEXT,
'notNull' => true,
'size' => 1,
'after' => 'id'
]
),
new Column(
'created_by',
[
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'size' => 10,
'after' => 'text'
]
)
],
'indexes' => [
new Index('PRIMARY', ['id'], 'PRIMARY'),
new Index('FK_notes_users', ['created_by'], null)
],
'references' => [
new Reference(
'FK_notes_users',
[
'referencedTable' => 'users',
'referencedSchema' => 'test',
'columns' => ['created_by'],
'referencedColumns' => ['id'],
'onUpdate' => 'RESTRICT',
'onDelete' => 'RESTRICT'
]
)
],
'options' => [
'TABLE_TYPE' => 'BASE TABLE',
'AUTO_INCREMENT' => '1',
'ENGINE' => 'InnoDB',
'TABLE_COLLATION' => 'utf8_general_ci'
],
]
);
}
}
class UsersMigration_100 extends Migration
{
public function morph()
{
$this->morphTable('users', [
'columns' => [
new Column(
'id',
[
'type' => Column::TYPE_INTEGER,
'unsigned' => true,
'notNull' => true,
'autoIncrement' => true,
'size' => 10,
'first' => true
]
),
new Column(
'name',
[
'type' => Column::TYPE_VARCHAR,
'notNull' => true,
'size' => 50,
'after' => 'id'
]
)
],
'indexes' => [
new Index('PRIMARY', ['id'], 'PRIMARY')
],
'options' => [
'TABLE_TYPE' => 'BASE TABLE',
'AUTO_INCREMENT' => '1',
'ENGINE' => 'InnoDB',
'TABLE_COLLATION' => 'utf8_general_ci'
],
]
);
}
} The reason is that migration files are processed in alphabetical order. You can take code for reproduce here https://github.com/topotru/phalcon-migrations-test.git Thank you for your contributing! |
@topotru This problem because referenced table isn't exist. You can add foreign key after creating second table. |
Do you suggest creating another migration file to add foreign keys? May be it's more correct to make a collection of keys when executing the migration version folder and apply it at once for all tables after completion for each version? |
Or maybe will be better to realise command line option like --foreign-key-check=false (by default - true) on migration run command. |
Also I want to note that the migrations generated by the "generate" command will also be invalid. |
@sergeyklay what do you think about it? |
@topotru At the moment migration generate foreign key in |
We only accept bug reports, new feature requests and pull requests in GitHub. For questions regarding the usage of the DevTools, support requests, and questions like this please visit the official forums. |
What are you talking about?! I expect that the migration files generated by the "migration generate" command can be executed by the command "migration run" without any changes, and execute without any errors. |
@topotru Actually migration works fine. Foreign key is very specific object. You have to take care about integrity of keys by yourself. There're many way to solve your problem: for example - you can execute SQL code for creating foreign key after running migration, or you can execute |
hello I am using the 2.0.8 version and I have this problem. The following code
throught the phalcon migration run command will produce a foreign key with reference option = NO ACTION for both update and delete.
The text was updated successfully, but these errors were encountered: