Show DEFERRABLE INITIALLY on foreign keys - #365
Merged
Conversation
PostgreSQL supports deferrable constraints on foreign keys. Rails' ForeignKeyDefinition exposes this via #deferrable, but annotaterb was ignoring it. Emit `DEFERRABLE INITIALLY IMMEDIATE|DEFERRED` alongside the existing ON DELETE / ON UPDATE metadata. Rails 7.0's extract_foreign_key_deferrable returns `true` for the initially-immediate case (7.1+ normalized this to `:immediate`), so map `true` back to IMMEDIATE to avoid emitting "INITIALLY TRUE".
OdenTakashi
approved these changes
Jul 23, 2026
OdenTakashi
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for adding this. Looks great to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Emit
DEFERRABLE INITIALLY IMMEDIATE|DEFERREDon foreign key annotations, alongside the existingON DELETE/ON UPDATEmetadata. Related to #364.Motivation
Rails exposes deferrable foreign keys via
ForeignKeyDefinition#deferrable, but annotaterb was silently dropping the flag — a foreign key created withadd_foreign_key ..., deferrable: :deferredwas indistinguishable from a plain one in the annotation.Compatibility
Rails 7.0's
extract_foreign_key_deferrablereturnstruefor the initially-immediate case (7.1+ normalized this to:immediate); mappedtrue→IMMEDIATEfor stable output. Guarded withrespond_to?(:deferrable)for older Rails / other adapters.