-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SHOW CREATE TABLE
returns invalid syntax for FK workaround
#42400
Comments
Unfortunately, the problem is that
should never have worked, because we don't attempt to support multiple foreign key constraints on the same column, which is the deficiency being tracked in #38850. I think the "workaround" mentioned in #38850 is actually the workaround of creating a duplicate of the column for each additional foreign key required, which is supported in Cockroach (by, for example, using computed columns), though it's inconvenient. @jordanlewis are you aware of any other workarounds for this? The behavior being reported is due to a bug where we we failed to include foreign keys on the primary key columns when checking that no column has more than one foreign key constraint. This was patched in 19.2, and I wouldn't recommend relying on the erroneous behavior in 19.1, even if there are no apparent errors when creating the table. We've run into this before (#37590 (comment)). Maybe we should backport a fix into 19.1. |
I opened #42486 to track backporting a fix to 19.1 to close the loophole. |
Fair enough; though I can’t help but wonder what the impact might be to installations that have already used this soon to be unsupported workaround. To be sure, redundant foreign key relationships raise questions about the quality of the data model, but... to revoke functionality on principle? Seems like an aggressive choice. Fortunately for me, I’m still writing my conversions from Postgres to cockroach (so I’ve got time to refactor), but I have to assume there’s someone who’s got an existing redundant reference you’re proposing to disallow 😬 |
This is a good point. We're already planning to add support for multiple FK constraints on the same column soon (ideally in 20.1, though it's not guaranteed), so the only question is what to do in the meantime. Based on what I've gathered from conversations with @knz, @jordanlewis, and others, there aren't any known problems in theory with multiple FK constraints from the same column. The restriction was put in place in a time when FKs were represented differently, and it wasn't clear that there was user demand, but in theory it should be able to be lifted now. The problem is that this functionality is untested and likely has unexpected behavior in edge cases, so any "partial support" for it via the loophole is potentially bad. If a user had a table schema that bypassed this restriction, then it should probably continue to work in 19.2 just as well as it did in 19.1. (I realized there's an additional complication in this case, though, which is that the fix introduced in 19.2 to close the loophole inadvertently also prevents new foreign keys from being added if a user already has foreign keys that bypass the restriction. This is because the check indiscriminately applies to both new and old foreign keys. It's easy enough to get out of this state by just dropping one of the duplicate FKs, but it just adds to the overall state of bugginess.) I think there are a few ways for us to proceed, until the restrictions are lifted in some later release:
There are other variants (e.g., we could backport a fix to 19.2 to close the loophole for new FKs while fixing the unintended consequences for exising FKs that violate the restriction, thus addressing the complication described above). I think (3) would be best, in the interest of avoiding potential undefined behavior as much as possible. But I'll defer to @jordanlewis or someone else on the SQL team. |
After discussing in SQL Features planning, our plan is to:
|
Zendesk ticket #4291 has been linked to this issue. |
We now allow multiple FK constraints on the same column in 19.2, and we've decided to not backport a fix to 19.1 (either to lift the restriction or to uniformly enforce it correctly). |
Describe the problem
Inline foreign key creation bails with the following error when a user attempts to create a redundant foreign key reference.
It is still possible to create a redundant (or partially redundant) reference using a workaround as noted here: #38850
However, examining the DDL state using
SHOW CREATE TABLE
after using such a workaround returns a command that cannot be executed to create the table.To Reproduce
Consider the following valid postgresql schema
When creating
table d
, user is required to use a workaround due to the redundant foreign key references.Foreign Key
d_b
is detected to be a partial duplicate ofd_b
and is rejected. However, when we inspect the current state ofd
, we see......which is not valid syntax for this version...
Expected behavior
Output from
SHOW CREATE TABLE
should be a valid executable syntax.Additional data
Environment:
cockroach sql
pg_dump
cockroach dump
Additional context
This was encountered when migrating an existing postgresql schema to cockroachdb.
Possibly related: #12718
The text was updated successfully, but these errors were encountered: