I create migration:
public function up() { $this->createTable('script2webpage', [ 'script_id' => $this->integer(11)->unsigned()->notNull(), 'page_id' => $this->integer(11)->unsigned()->notNull(), ]); $this->addForeignKey('FK_ITEM_SCRIPT','script2webpage', 'script_id','scripts','id'); $this->addForeignKey('FK_ITEM_WEBPAGE','script2webpage', 'page_id','webpage','page_id'); } public function down() { $this->dropForeignKey('FK_ITEM_SCRIPT', 'script2webpage'); $this->dropForeignKey('FK_ITEM_WEBPAGE', 'script2webpage'); $this->dropTable('script2webpage'); } Migration error
Error Info: Array ( [0] => HY000 [1] => 1005 [2] => Can't create table 'ms.#sql-1ed4_2e' (errno: 150) ) The first FK is created. On 2 error:
add foreign key FK_ITEM_WEBPAGE: script2webpage (page_id) references webpage (page_id) ...Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1005 Can't create table 'tablename.#sql-1ed4_2e' (errno: 150) The SQL being executed was: ALTER TABLE `script2webpage` ADD CONSTRAINT `FK_ITEM_WEBPAGE` FOREIGN KEY (`page_id`) REFERENCES `webpage` (`page_id`)' Perhaps this is due to the table engines:
CREATE TABLE ms.scripts ( id int(10) UNSIGNED NOT NULL AUTO_INCREMENT, title varchar(50) NOT NULL, code text DEFAULT NULL, url varchar(255) DEFAULT NULL, for_all tinyint(1) DEFAULT NULL, status tinyint(1) DEFAULT NULL, description text DEFAULT NULL, created_at datetime DEFAULT NULL, updated_at datetime DEFAULT NULL, PRIMARY KEY (id) ) ENGINE = INNODB, CHARACTER SET utf8, COLLATE utf8_general_ci; CREATE TABLE ms.webpage ( page_id int(11) UNSIGNED NOT NULL AUTO_INCREMENT, title varchar(255) DEFAULT NULL, url varchar(128) NOT NULL, body text DEFAULT NULL, layout varchar(72) DEFAULT 'main', menu_title varchar(255) DEFAULT NULL, seo_title varchar(255) DEFAULT NULL, seo_description text DEFAULT NULL, seo_keywords text DEFAULT NULL, status tinyint(1) UNSIGNED NOT NULL DEFAULT 1, PRIMARY KEY (page_id) ) ENGINE = MYISAM, AUTO_INCREMENT = 15, AVG_ROW_LENGTH = 1806, CHARACTER SET utf8, CHECKSUM = 0, COLLATE utf8_general_ci; ALTER TABLE ms.webpage ADD UNIQUE INDEX url (url); How in this case to get rid of the error?