Skip to content

Expose foreign key constraint options in inline FK definition and in create_constraint #653

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

Merged
merged 2 commits into from
Feb 6, 2025

Conversation

kvch
Copy link
Contributor

@kvch kvch commented Feb 4, 2025

Now more foreign key options are supported:

  • match_type in column definitions and in create_constraint
  • on_update in column definitions and in create_constraint
  • on_delete_set_columns in create_constraint
  • on_update in inline FK definitions in a column's references
  • match_type in inline FK definction in a column's references

Extracted from #628. The only remaining part of that PR is adding support in sql2pgroll. 🎉

@kvch kvch force-pushed the feature-use-fk-constraints-everywhere branch from 593deaa to 0afae1d Compare February 4, 2025 16:21
@kvch kvch changed the title Expose foreign key constraint options Expose foreign key constraint options in inline FK definition and in create_constraint Feb 4, 2025
@kvch kvch marked this pull request as ready for review February 4, 2025 17:24
@kvch kvch requested a review from andrew-farries February 4, 2025 17:24
@kvch
Copy link
Contributor Author

kvch commented Feb 4, 2025

I am adding more tests.

New options are added to inline FK constraints
and to `create_constraint` operation
@kvch kvch force-pushed the feature-use-fk-constraints-everywhere branch from 661b56c to 9196f82 Compare February 5, 2025 13:58
@kvch
Copy link
Contributor Author

kvch commented Feb 5, 2025

I rebased the branch and added tests.

@@ -404,8 +404,10 @@ func (w *ConstraintSQLWriter) WriteForeignKey(referencedTable string, referenced
if w.Name != "" {
constraint = fmt.Sprintf("CONSTRAINT %s ", pq.QuoteIdentifier(w.Name))
}
constraint += fmt.Sprintf("FOREIGN KEY (%s) REFERENCES %s (%s) MATCH %s ON DELETE %s ON UPDATE %s",
strings.Join(quoteColumnNames(w.Columns), ", "),
if len(w.Columns) != 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under what circumstances would we have 0 columns here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of inline FK constraints. I am adding a comment so it's clear.

@kvch kvch enabled auto-merge (squash) February 6, 2025 16:21
@kvch kvch merged commit 09e2fd4 into xataio:main Feb 6, 2025
27 checks passed
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

Successfully merging this pull request may close these issues.

2 participants