Skip to content

Commit

Permalink
Merge pull request #382 from humhub/fix/migrate-add-foreign-key
Browse files Browse the repository at this point in the history
Fix migration with adding foreign keys
  • Loading branch information
luke- authored Feb 1, 2025
2 parents 0b16307 + 89c43be commit 6e2e048
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions migrations/m250129_065352_content_definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ public function safeDown()

return false;
}

/**
* @inheritdoc
*/
protected function safeAddForeignKey(string $index, string $table, $columns, string $refTable, $refColumns, ?string $delete = null, ?string $update = null)
{
if (is_string($columns) && is_string($refColumns) && !str_contains($columns . $refColumns, ',')) {
// Delete wrong records if they haven't got a linked record in the ref table
$this->db
->createCommand()
->delete($table, $this->db->quoteColumnName($columns) . ' IS NOT NULL AND ' .
$this->db->quoteColumnName($columns) . ' NOT IN (SELECT ' . $this->db->quoteColumnName($refColumns) . ' FROM ' . $this->db->quoteTableName($refTable) . ')')
->execute();
}

return parent::safeAddForeignKey($index, $table, $columns, $refTable, $refColumns, $delete, $update);
}
}

0 comments on commit 6e2e048

Please sign in to comment.