From d6297d44d5f056ea0133f380c7dd46e9343fd3ed Mon Sep 17 00:00:00 2001 From: Adrien Berchet Date: Wed, 10 Aug 2022 23:26:28 +0200 Subject: [PATCH] Fix create_geo_table() in alembic helpers to handle foreign keys (#398) --- geoalchemy2/alembic_helpers.py | 8 +++++++- tests/test_alembic_migrations.py | 29 +++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/geoalchemy2/alembic_helpers.py b/geoalchemy2/alembic_helpers.py index c432cb92..1726c498 100644 --- a/geoalchemy2/alembic_helpers.py +++ b/geoalchemy2/alembic_helpers.py @@ -462,7 +462,13 @@ def create_geo_table(context, revision, op): ) if gis_cols: - new_op = CreateGeospatialTableOp(op.table_name, op.columns, op.schema) + new_op = CreateGeospatialTableOp( + op.table_name, + op.columns, + op.schema, + op._namespace_metadata, + op._constraints_included, + ) else: new_op = op diff --git a/tests/test_alembic_migrations.py b/tests/test_alembic_migrations.py index 77a41eb9..a4c3b316 100644 --- a/tests/test_alembic_migrations.py +++ b/tests/test_alembic_migrations.py @@ -272,18 +272,28 @@ def test_migration_revision( with test_script_path.open(mode="w", encoding="utf8") as f: f.write( """ -from sqlalchemy import MetaData +from geoalchemy2 import Geometry from sqlalchemy import Column +from sqlalchemy import ForeignKey from sqlalchemy import Integer +from sqlalchemy import MetaData +from sqlalchemy import String from sqlalchemy import Table -from geoalchemy2 import Geometry metadata = MetaData() +group_table = Table( + "new_groups", + metadata, + Column("id", Integer, primary_key=True, autoincrement=True), + Column("name", String), +) + new_table = Table( "new_spatial_table", metadata, Column("id", Integer, primary_key=True), + Column("group_id", Integer, ForeignKey(group_table.c.id)), Column( "geom_with_idx", Geometry( @@ -371,18 +381,29 @@ def test_migration_revision( with test_script_path.open(mode="w", encoding="utf8") as f: f.write( """ -from sqlalchemy import MetaData +from geoalchemy2 import Geometry from sqlalchemy import Column +from sqlalchemy import ForeignKey from sqlalchemy import Integer +from sqlalchemy import MetaData +from sqlalchemy import String from sqlalchemy import Table -from geoalchemy2 import Geometry metadata = MetaData() +group_table = Table( + "new_groups", + metadata, + Column("id", Integer, primary_key=True, autoincrement=True), + Column("name", String), +) + + new_table = Table( "new_spatial_table", metadata, Column("id", Integer, primary_key=True), + Column("group_id", Integer, ForeignKey(group_table.c.id)), Column( "geom_with_idx", Geometry(