-
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
sql: column names are not updated in the foreign keys when renaming a column #42208
Comments
Lucy would you be able to take a first look at diagnosing this? |
On first glance, the problem seems likely to be with
@jordanlewis does anything seem suspicious to you with |
I tried debugging it briefly, looked into how |
Incorrect in the sense that it returned two constraints where there should only have been one? |
Incorrect in a sense that it contained old column name (but, to be honest, I didn't look into that deep enough, so it may be better to ignore my comments). |
edit: that wasn't it |
okay, the reason is because postgres names their FKs constraints differently to how we name it. Postgres names: We don't (and psql doesn't afaict) rename constraints on columns renames. Since they both collide, this check will reference the I think the correct fix for this is to change the query (but we can also change names as well; they just won't backport well):
|
If I understand correctly, the problem is that the names of FKs referencing the same table are not unique, right? (This problem could come up even with user-provided constraint names.) In that case, I think that fix should work. Thanks for looking into this. |
Yep! Not sure if we should close :) but it seems like a case of the query being able to be more exact instead |
I originally thought that query was an internal one when I first looked at this, but now I'm realizing it's presumably in the ORM. Is updating the query actually viable? Even if we do that for this particular ORM, I'm worried we'll run into more compatibility problems if other tools are assuming that FK names are unique on the referencing side. |
Maybe it is worth looking at changing constraint names in general to be more exact. I think any ORM trying to discern FKs by name instead of referencing columns may run into issues though -- given that people can name constraints themselves as well. Not sure how many ORMs can assume this / whether we can tell them to use alternative methods though... |
@rafiss can you take a look at this and then work with PeeWee on seeing whether this is a problem and if so how we can fix it? |
@coleifer Hi! Would you be able to let us know if PeeWeeORM still is encountering an issue here? To summarize the discussion above, if there is still a problem, I will be able to help you come up with an alternate query to find foreign keys that will work better with CockroachDB. |
@otan / @lucy-zhang - do you have a suggestion for a more correct way to introspect the foreign-key constraints for a given table, besides the additional join predicates? @rafiss - the suggested change seems to be working well and the tests that were previously failing now pass. |
@coleifer You have a couple options for making this query.
As far as performance is concerned, it doesn't really matter which option you pick. |
Yes, I implemented the latter, moving the additional predicates into the join - but what I meant was: is there another way altogether of introspecting this that the cockroachdb devs would recommend? Related commit for peewee: coleifer/peewee@d9a5839 |
This way seems fine. There are several other ways to get the foreign keys of a table, but I don't see any reason to change what you have. You could take a look at what other ORMs do if you're curious. To be clear though, this issue is not related to renaming the column -- it's caused by the different choice that CockroachDB makes for the default constraint name as compared to PostgreSQL. Because of that, you may want similar logic for PostgreSQL too, since it lets you rename or customize the names of constraints. For example, if you have this in this PostgreSQL, the original query in this ticket would return incorrect results.
Thanks for making that patch! I'll go ahead and close this issue, but feel free to keep discussing if you have further questions. |
Our contractor working on PeeWee ORM ran into a bug. It seems to me that when we're renaming a column, not all places are updated correctly - in particular, foreign keys seem to be forgotten. Here is a repro:
Currently on master we return:
whereas Postgres returns:
The text was updated successfully, but these errors were encountered: