When creating a migration, I want to make sure that such a foreign key does not exist.

$this->createForeignKey( self::TABLE_NAME, self::COLUMN, self::FOREIGN_TABLE_NAME, 'id', 'SET NULL', 'CASCADE' ); 
  • Where can he come from? - etki
  • in case of manual addition / error when adding / deleting - des1roer
  • Do you need to consider adding manually when using migrations? Do you need to do such a migration, which will apply only half? - etki
  • Now such a problem - there is no fk on the combat base, there are no maidens. how this happened is incomprehensible. I need the same behavior on both the prode and the maiden. those. you need to check on the maiden whether there is already fc - des1roer
  • To avoid such situations, it is better to do the Makarov method: call indices as idx-table_name-column , foreign keys as fk-table_name-column , or even fk-table_name-column-ref_table-ref_columnn - pa3py6aka

1 answer 1

 public function safeUp() { Yii::$app->db->schema->refreshTableSchema(self::TABLE_NAME); $tableSchema = Yii::$app->db->schema->getTableSchema(self::TABLE_NAME, true); if (!in_array( self::COLUMN, array_keys($tableSchema->foreignKeys) )) { $this->createForeignKey( self::TABLE_NAME, self::COLUMN, self::FOREIGN_TABLE_NAME, 'id', 'SET NULL', 'CASCADE' ); } }