Migrations : fix ADD CONSTRAINT IF NOT EXISTS (syntaxe PostgreSQL invalide)
Remplace par un bloc DO $$ IF NOT EXISTS ... $$ pour le FK circulaire item_versions→document_items. Testé localement sur pg-test : 5 migrations OK + idempotence OK. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -232,9 +232,18 @@ def upgrade() -> None:
|
|||||||
""")
|
""")
|
||||||
|
|
||||||
op.execute("""
|
op.execute("""
|
||||||
ALTER TABLE item_versions
|
DO $$
|
||||||
ADD CONSTRAINT IF NOT EXISTS item_versions_item_id_fkey
|
BEGIN
|
||||||
FOREIGN KEY (item_id) REFERENCES document_items(id)
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.table_constraints
|
||||||
|
WHERE constraint_name = 'item_versions_item_id_fkey'
|
||||||
|
AND table_name = 'item_versions'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE item_versions
|
||||||
|
ADD CONSTRAINT item_versions_item_id_fkey
|
||||||
|
FOREIGN KEY (item_id) REFERENCES document_items(id);
|
||||||
|
END IF;
|
||||||
|
END $$
|
||||||
""")
|
""")
|
||||||
|
|
||||||
op.execute("""
|
op.execute("""
|
||||||
|
|||||||
Reference in New Issue
Block a user