diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2019-08-19 04:41:28 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@bbpush.zzzcomputing.com> | 2019-08-19 04:41:28 +0000 |
| commit | 512f045e9e70429d75811113f8e2b69842cc4406 (patch) | |
| tree | 7e6530f37489103bfea71daeebc4571c65427895 /lib/sqlalchemy | |
| parent | effb47348c2758fa354957b647420ddf129e899e (diff) | |
| parent | f06c6ba67303e5c75d8ad044494193d8f97b2e2e (diff) | |
| download | sqlalchemy-512f045e9e70429d75811113f8e2b69842cc4406.tar.gz | |
Merge "Reflect PK of referred table if referred columns not present"
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/dialects/sqlite/base.py | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 78ce18ac6..7be0e06dc 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -1754,8 +1754,22 @@ class SQLiteDialect(default.DefaultDialect): for row in pragma_fks: (numerical_id, rtbl, lcol, rcol) = (row[0], row[2], row[3], row[4]) - if rcol is None: - rcol = lcol + if not rcol: + # no referred column, which means it was not named in the + # original DDL. The referred columns of the foreign key + # constraint are therefore the primary key of the referred + # table. + referred_pk = self.get_pk_constraint( + connection, rtbl, schema=schema, **kw + ) + # note that if table doesnt exist, we still get back a record, + # just it has no columns in it + referred_columns = referred_pk["constrained_columns"] + else: + # note we use this list only if this is the first column + # in the constraint. for subsequent columns we ignore the + # list and append "rcol" if present. + referred_columns = [] if self._broken_fk_pragma_quotes: rtbl = re.sub(r"^[\"\[`\']|[\"\]`\']$", "", rtbl) @@ -1768,13 +1782,15 @@ class SQLiteDialect(default.DefaultDialect): "constrained_columns": [], "referred_schema": schema, "referred_table": rtbl, - "referred_columns": [], + "referred_columns": referred_columns, "options": {}, } fks[numerical_id] = fk fk["constrained_columns"].append(lcol) - fk["referred_columns"].append(rcol) + + if rcol: + fk["referred_columns"].append(rcol) def fk_sig(constrained_columns, referred_table, referred_columns): return ( |
