Skip to content

Commit 0a7010b

Browse files
authored
Made read_postgis raise ValueError if the geom_col is specified twice (current error is cryptic) (#2849)
1 parent cd844e6 commit 0a7010b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

geopandas/io/sql.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def _df_to_geodf(df, geom_col="geom", crs=None):
6868
if geom_col not in df:
6969
raise ValueError("Query missing geometry column '{}'".format(geom_col))
7070

71+
if df.columns.to_list().count(geom_col) > 1:
72+
raise ValueError(
73+
f"Duplicate geometry column '{geom_col}' detected in SQL query output. Only"
74+
"one geometry column is allowed."
75+
)
76+
7177
geoms = df[geom_col].dropna()
7278

7379
if not geoms.empty:

geopandas/io/tests/test_sql.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,3 +734,14 @@ def test_append_with_different_crs(self, engine_postgis, df_nybb):
734734
# Should raise error when appending
735735
with pytest.raises(ValueError, match="CRS of the target table"):
736736
write_postgis(df_nybb2, con=engine, name=table, if_exists="append")
737+
738+
def test_duplicate_geometry_column_fails(self, engine_postgis):
739+
"""
740+
Tests that a ValueError is raised if an SQL query returns two geometry columns.
741+
"""
742+
engine = engine_postgis
743+
744+
sql = "select ST_MakePoint(0, 0) as geom, ST_MakePoint(0, 0) as geom;"
745+
746+
with pytest.raises(ValueError):
747+
read_postgis(sql, engine, geom_col="geom")

0 commit comments

Comments
 (0)