Skip to content

Conversation

@Martin-Alexander
Copy link
Contributor

@Martin-Alexander Martin-Alexander commented Jul 18, 2025

Closes: #174

This fixes the following methods:

  • add/remove_foreign_key
  • add/remove_reference
  • create_table
  • change_table

The basic idea is that when we want to create a foreign key between a from_table and to_table we ask:

  • Is to_table already schema qualified? If so, leave it as it.
  • Do to_table and from_table both belong to the same schema? If so, leave them as is.
  • Otherwise, we qualify to_table with the schema that it should belong to.

The methods #add_foreign_key and #remove_foreign_key are used under the hood by #add_reference, #remove_reference, and #change_table so the fix works for all of them.

A similar fix is added for TableDefinition#foreign_key that is used in #create_table

Knowing what schema a foreign key constraint should be on requires knowing what the database's "default" schema was (i.e., the current schema before we manipulated it by calling #on_temporal_schema or #on_history_schema). So this modifies #on_schema to keep track of that.

@tagliala Let me know what you think of this approach and if it looks good I can address some of the outstanding issues and clean up the code a bit.

Todo:

  • Handle the case where #remove_foreign_key is given nil for the to_table keyword argument
  • Refactor/clean up the repetition in the code
  • Write tests to ensure schema quoting is properly handled
  • Fix style offenses

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.

add/remove_foreign_key do not work on temporal tables

1 participant