Skip to content
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

Postgres foreign keys not working if name is the same #396

Closed
jcomx opened this issue Jun 29, 2023 · 2 comments
Closed

Postgres foreign keys not working if name is the same #396

jcomx opened this issue Jun 29, 2023 · 2 comments

Comments

@jcomx
Copy link

jcomx commented Jun 29, 2023

Hi, I start working in a new project with Postgres and might found an issue with the foreign keys generated.

When this happens?
I only have seen this issue when the constraint name is the same in multiple tables. i.e.

Table Orders
FK fk_customer_id
Table Addresses
FK fk_customer_id

Current result:
This is generating foreign keys functions with wrong names i.e.

return CustomerByCustomerIDByCustomerID(....

Expected result:

return CustomerByCustomerID(...

My findings:
After read the code, I think the issue might be solved in the SQL Query that retrieve the foreign keys
Filename: models/foreignkey.xo.go
Function: PostgresTableForeignKeys

		...
		`FROM information_schema.table_constraints tc ` +
		`JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name ` +
		`AND tc.table_schema = kcu.table_schema ` +
		// here can be added the new condition to match the table name
		// `AND tc.table_name = kcu.table_name` +
		`JOIN ( ` +
		...

If I can support in anything else please let me know. (issue description, testing, etc.)

Best Regards.

evassilyev added a commit to evassilyev/xo that referenced this issue Oct 29, 2023
@evassilyev
Copy link
Contributor

@jcomx Julio, thank you for the detailed description and proposed solution.
I've prepared PR with your fixes: PR 401.
I hope @kenshaw will handle it soon.

@evassilyev
Copy link
Contributor

@jcomx fixes have been merged, I guess you can close the issue already.

Additional PR with examples for reproduce the issue:
402

@jcomx jcomx closed this as completed Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants