Skip to content

Commit 77c7737

Browse files
Fix: Fix the query to check indexes and fix tests
1 parent 35b4502 commit 77c7737

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

geoalchemy2/admin/dialects/postgresql.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,32 @@ def reflect_geometry_column(inspector, table, column_info):
7272
else:
7373
schema_part = ""
7474

75-
has_index_query = """SELECT (indexrelid IS NOT NULL) AS has_index
76-
FROM (
77-
SELECT
78-
n.nspname,
79-
c.relname,
80-
c.oid AS relid,
81-
a.attname,
82-
a.attnum
83-
FROM pg_attribute a
84-
INNER JOIN pg_class c ON (a.attrelid=c.oid)
85-
INNER JOIN pg_type t ON (a.atttypid=t.oid)
86-
INNER JOIN pg_namespace n ON (c.relnamespace=n.oid)
87-
WHERE c.relkind='r'
88-
) g
89-
LEFT JOIN pg_index i ON (g.relid = i.indrelid AND g.attnum = ANY(i.indkey))
90-
WHERE relname = '{}' AND attname = '{}'{};
91-
""".format(
92-
table.name, column_info["name"], schema_part
75+
# Check if the column has a spatial index (the regular expression checks for the column name
76+
# in the index definition, which is required for functional indexes)
77+
has_index_query = """SELECT EXISTS (
78+
SELECT 1
79+
FROM pg_class t
80+
JOIN pg_namespace n ON n.oid = t.relnamespace
81+
JOIN pg_index ix ON t.oid = ix.indrelid
82+
JOIN pg_class i ON i.oid = ix.indexrelid
83+
JOIN pg_am am ON i.relam = am.oid
84+
WHERE
85+
t.relname = '{table_name}'{schema_part}
86+
AND am.amname = 'gist'
87+
AND (
88+
EXISTS (
89+
SELECT 1
90+
FROM pg_attribute a
91+
WHERE a.attrelid = t.oid
92+
AND a.attnum = ANY(ix.indkey)
93+
AND a.attname = '{col_name}'
94+
)
95+
OR pg_get_indexdef(
96+
ix.indexrelid
97+
) ~ '(^|[^a-zA-Z0-9_])("?{col_name}"?)($|[^a-zA-Z0-9_])'
98+
)
99+
);""".format(
100+
table_name=table.name, col_name="rast", schema_part=schema_part
93101
)
94102
spatial_index = inspector.bind.execute(text(has_index_query)).scalar()
95103

tests/schema_fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class Lake(base):
198198
geom_geog_no_idx = Column(
199199
Geography(geometry_type="LINESTRING", spatial_index=False)
200200
)
201-
rast = Column(Raster())
201+
rast = Column(Raster(spatial_index=True))
202202
rast_no_idx = Column(Raster(spatial_index=False))
203203

204204
return metadata

tests/test_functional.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,18 @@ def test_reflection(self, conn, setup_reflection_tables, dialect_name):
12431243
assert type_.srid == 4326
12441244
assert type_.dimension == 2
12451245

1246+
type_ = t.c.rast.type
1247+
assert isinstance(type_, Raster)
1248+
assert type_.geometry_type is None
1249+
assert type_.srid == -1
1250+
assert type_.dimension is None
1251+
1252+
type_ = t.c.rast_no_idx.type
1253+
assert isinstance(type_, Raster)
1254+
assert type_.geometry_type is None
1255+
assert type_.srid == -1
1256+
assert type_.dimension is None
1257+
12461258
# Drop the table
12471259
t.drop(bind=conn)
12481260

0 commit comments

Comments
 (0)