Пытаюсь связать 2 таблицы, таблицу пользователя и ранга.
В миграциях Yii2, по порядку:
Таблица пользователей:
$this->createTable('{{%user}}', [
'id' => Schema::TYPE_INTEGER . ' NOT NULL AUTO_INCREMENT',
'username' => Schema::TYPE_STRING . ' NOT NULL',
'level' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
'PRIMARY KEY (id)',
], $tableOptions);
Таблица рангов:
$this->createTable('{{%ranks}}', [
'level' => Schema::TYPE_INTEGER . ' NOT NULL DEFAULT 1',
'rank' => Schema::TYPE_STRING . ' NOT NULL',
'exp' => Schema::TYPE_INTEGER . ' NOT NULL'
], $tableOptions);
$this->addForeignKey("ranks_fk", "{{%ranks}}", "level", "{{%user}}", "level", 'RESTRICT');
Выдает ошибку:
> add foreign key ranks_fk: {{%ranks}} (level) references {{%user}} (level) ...Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
The SQL being executed was: ALTER TABLE `ranks` ADD CONSTRAINT `ranks_fk` FOREIGN KEY (`level`) REFERENCES `user` (`level`) ON DELETE RESTRICT'
in C:\OpenServer\domains\site-dev\vendor\yiisoft\yii2\db\Schema.php:503
Error Info:
Array
(
[0] => HY000
[1] => 1215
[2] => Cannot add foreign key constraint
)
Подскажите, в чем проблема?