UniqueConstraint and Index withheld from table via attach_to_table flag, and appended to table via append_constraint by bekapono · Pull Request #13399 · sqlalchemy/sqlalchemy · GitHub
Skip to content

UniqueConstraint and Index withheld from table via attach_to_table flag, and appended to table via append_constraint#13399

Open
bekapono wants to merge 5 commits into
sqlalchemy:mainfrom
bekapono:flag_attach_to_table
Open

UniqueConstraint and Index withheld from table via attach_to_table flag, and appended to table via append_constraint#13399
bekapono wants to merge 5 commits into
sqlalchemy:mainfrom
bekapono:flag_attach_to_table

Conversation

@bekapono

@bekapono bekapono commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Description

attach_to_table has been included for UniqueConstraint and Index to allow users to choose whether to include or withhold them from the Table (Table.constraints for UniqueConstraint, Table.indexes for Index). attach_to_table defaults to True for both UniqueConstraint and Index. So if set to True or not included UniqueConstraint / Index will be added to the Table.

For UniqueConstraint, setting attach_to_table to False prevents parent.constraints.add(self) from executing in Column._set_parent(). attach_to_table is checked via getattr
For Index, setting attach_to_table to False prevents table.indexes.add(self) from executing in Index._set_parent()

force_attach has been included in Table.append_constraints(), and carried via **kw , to allow users to override attach_to_table. For situations where UniqueConstraint or Index was defined before the Table creation and attach_to_table is set to False, users can use that same UniqueConstraint or Index to append to table at a later time. For example unique_constraint = UniqueConstraint("x", attach_to_table=False) or index = Index("x", attach_to_table=False), then Table is defined, then Table.append_constraint(unique_constraint) or Table.append_constraint(index).

For UniqueConstraint, regardless of attach_to_table, using Table.append_constraint(constriant) allows for execution of parent.constraints.add(self) in Column._set_parent().
For Index, regardless of attach_to_table, using Table.append_constraint(index) allows for execution of table.indexes.add(self) in Index._set_parent().

Fixes: #13048

Checklist

This pull request is:

  • A documentation / typographical / small typing error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

Have a nice day!

@bekapono

Copy link
Copy Markdown
Contributor Author

@zzzeek

zzzeek commented Jun 30, 2026

Copy link
Copy Markdown
Member

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 option for disabling automatic addition of constraint/indexes to the table

2 participants