I can not create a migration
public function up() { $this->createTable('service_category2service', [ 'service_category_id' => $this->integer(11)->unsigned()->notNull(), 'service_id' => $this->integer(11)->unsigned()->notNull(), ]); $this->addForeignKey('FK_ITEM_SC2S_PARENT', 'service_category2service', ['service_category_id'], \app\models\Service::tableName(), ['service_id']); $this->addForeignKey('FK_ITEM_SC2S_SERVICE', 'service_category2service', ['service_id'], \app\models\Service::tableName(), ['service_id']); $this->createIndex('UK_SC2S', 'service_category2service', ['service_category_id', 'service_id'], true); } public function down() { $this->dropForeignKey('FK_ITEM_SC2S_PARENT','service_category2service'); $this->dropForeignKey('FK_ITEM_SC2S_SERVICE','service_category2service'); $this->dropTable(\app\models\ServiceCategory2service::tableName()); } I attach the migration for the services table:
$this->createTable('services', [ 'service_id' => $this->primaryKey()->unsigned()->notNull(), 'title' => $this->string()->comment('Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Π΄Π»Ρ ΡΠΏΠΈΡΠΊΠ° ΡΡΠ»ΡΠ³'), 'url' => $this->string(128)->notNull()->unique(), 'icon' => $this->string()->comment('ΠΊΠ°ΡΡΠΈΠ½ΠΊΠ° Π΄Π»Ρ ΡΠΏΠΈΡΠΊΠ° ΡΡΠ»ΡΠ³'), 'bg_pic' => $this->string(), 'lead' => $this->text()->comment('ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ Π΄Π»Ρ ΡΠΏΠΈΡΠΊΠ° ΡΡΠ»ΡΠ³'), 'lead_full' => $this->text()->comment('ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ Π΄Π»Ρ ΡΠΏΠΈΡΠΊΠ° ΡΡΠ»ΡΠ³'), 'body' => $this->text(), 'seo_title' => $this->string(), 'seo_description' => $this->text(), 'seo_keywords' => $this->text(), 'sort' => $this->smallInteger()->unsigned()->notNull(), // ΡΠΊΡΡΡΠ°/ΠΎΠΏΡΠ±Π»ΠΈΠΊΠΎΠ²Π°Π½Π° 'status' => $this->boolean()->unsigned()->notNull()->defaultValue(1), ]); } Mistake
foreign key FK_ITEM_SC2S_PARENT: service_category2service (service_category_id) references services (service_id) ...Exception 'yii\db\Exception' with message 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint The SQL being executed was: ALTER TABLE `service_category2service` ADD CONSTRAINT `FK_ITEM_SC2S_PARENT` FOREIGN KEY (`service_category_id`) REFERENCES `services` (`service_id`)'