Skip to content

Commit 309c641

Browse files
authored
fix: Report column defaults in introspection (#744)
Retrieve the column default when introspecting columns. Currently, the default is hard-coded to None. Fixes: #730
1 parent 1f8a25f commit 309c641

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,8 @@ def get_multi_columns(
11221122

11231123
sql = """
11241124
SELECT col.table_schema, col.table_name, col.column_name,
1125-
col.spanner_type, col.is_nullable, col.generation_expression
1125+
col.spanner_type, col.is_nullable, col.generation_expression,
1126+
col.column_default
11261127
FROM information_schema.columns as col
11271128
JOIN information_schema.tables AS t
11281129
USING (TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME)
@@ -1150,7 +1151,7 @@ def get_multi_columns(
11501151
"name": col[2],
11511152
"type": self._designate_type(col[3]),
11521153
"nullable": col[4] == "YES",
1153-
"default": None,
1154+
"default": col[6] if col[6] is not None else None,
11541155
}
11551156

11561157
if col[5] is not None:

test/system/test_basics.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
)
3535
from sqlalchemy.orm import Session, DeclarativeBase, Mapped, mapped_column
3636
from sqlalchemy.types import REAL
37-
from sqlalchemy.testing import eq_, is_true, is_not_none
37+
from sqlalchemy.testing import eq_, is_true, is_not_none, is_none
3838
from sqlalchemy.testing.plugin.plugin_base import fixtures
3939

4040

@@ -47,7 +47,7 @@ def define_tables(cls, metadata):
4747
Column("number", Integer),
4848
Column("name", String(20)),
4949
Column("alternative_name", String(20)),
50-
Column("prime", Boolean),
50+
Column("prime", Boolean, server_default=text("FALSE")),
5151
Column("ln", REAL),
5252
PrimaryKeyConstraint("number"),
5353
)
@@ -120,12 +120,15 @@ def test_reflect(self, connection):
120120
eq_(5, len(table.columns))
121121
eq_("number", table.columns[0].name)
122122
eq_(BIGINT, type(table.columns[0].type))
123+
is_none(table.columns[0].server_default)
123124
eq_("name", table.columns[1].name)
124125
eq_(String, type(table.columns[1].type))
125126
eq_("alternative_name", table.columns[2].name)
126127
eq_(String, type(table.columns[2].type))
127128
eq_("prime", table.columns[3].name)
128129
eq_(Boolean, type(table.columns[3].type))
130+
is_not_none(table.columns[3].server_default)
131+
eq_("FALSE", table.columns[3].server_default.arg.text)
129132
eq_("ln", table.columns[4].name)
130133
eq_(REAL, type(table.columns[4].type))
131134
eq_(1, len(table.indexes))

0 commit comments

Comments
 (0)