Skip to content

Commit 8bb8c1a

Browse files
zzzeekGerrit Code Review
authored andcommitted
Merge "Fix get_columns sqlite reflection rejecting tables with WITHOUT_ROWID and/or STRICT for generated column case" into main
2 parents 5333228 + d526ced commit 8bb8c1a

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. change::
2+
:tags: bug, sqlite
3+
:tickets: 12864
4+
5+
Fixed issue where SQLite table reflection would fail for tables using
6+
``WITHOUT ROWID`` and/or ``STRICT`` table options when the table contained
7+
generated columns. The regular expression used to parse ``CREATE TABLE``
8+
statements for generated column detection has been updated to properly
9+
handle these SQLite table options that appear after the column definitions.
10+
Pull request courtesy Tip ten Brink.

lib/sqlalchemy/dialects/sqlite/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,10 @@ def get_columns(self, connection, table_name, schema=None, **kw):
23772377
)
23782378
# remove create table
23792379
match = re.match(
2380-
r"create table .*?\((.*)\)$",
2380+
(
2381+
r"create table .*?\((.*)\)"
2382+
r"(?:\s*,?\s*(?:WITHOUT\s+ROWID|STRICT))*$"
2383+
),
23812384
tablesql.strip(),
23822385
re.DOTALL | re.IGNORECASE,
23832386
)

test/dialect/test_sqlite.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,25 @@ def setup_test_class(cls):
39803980
x INTEGER GENERATED ALWAYS AS (INSTR(s, ",")) STORED,
39813981
y INTEGER GENERATED ALWAYS AS (INSTR(x, ",")) STORED
39823982
);""",
3983+
"""CREATE TABLE test9 (
3984+
id INTEGER PRIMARY KEY,
3985+
s VARCHAR,
3986+
x VARCHAR GENERATED ALWAYS AS (s || 'x')
3987+
) WITHOUT ROWID;""",
3988+
"""CREATE TABLE test10 (
3989+
s TEXT,
3990+
x TEXT GENERATED ALWAYS AS (s || 'x')
3991+
) STRICT;""",
3992+
"""CREATE TABLE test11 (
3993+
id INTEGER PRIMARY KEY,
3994+
s TEXT,
3995+
x TEXT GENERATED ALWAYS AS (s || 'x')
3996+
) STRICT, WITHOUT ROWID;""",
3997+
"""CREATE TABLE test12 (
3998+
id INTEGER PRIMARY KEY,
3999+
s TEXT,
4000+
x TEXT GENERATED ALWAYS AS (s || 'x')
4001+
) WITHOUT ROWID, STRICT;""",
39834002
]
39844003

39854004
with testing.db.begin() as conn:
@@ -4013,6 +4032,10 @@ def teardown_test_class(cls):
40134032
"x": {"text": 'INSTR(s, ",")', "stored": True},
40144033
"y": {"text": 'INSTR(x, ",")', "stored": True},
40154034
},
4035+
"test9": {"x": {"text": "s || 'x'", "stored": False}},
4036+
"test10": {"x": {"text": "s || 'x'", "stored": False}},
4037+
"test11": {"x": {"text": "s || 'x'", "stored": False}},
4038+
"test12": {"x": {"text": "s || 'x'", "stored": False}},
40164039
}
40174040

40184041
def test_reflection(self, connection):

0 commit comments

Comments
 (0)