Skip to content

1456 Update registration tables with comments - #4796

Merged
Sepehr-Sobhani merged 3 commits into
developfrom
1456-registration-app-tables-have-meaningful-metadata
Jun 20, 2026
Merged

1456 Update registration tables with comments#4796
Sepehr-Sobhani merged 3 commits into
developfrom
1456-registration-app-tables-have-meaningful-metadata

Conversation

@Sepehr-Sobhani

@Sepehr-Sobhani Sepehr-Sobhani commented Jun 12, 2026

Copy link
Copy Markdown
Member

ISSUE 1456

⚠️ This PR added approximately 7,000 new lines of code through the new migration file. We should consider squashing migrations again two or three releases after the latest ticket about adding metadata to the tables is completed.

What is SeparateDatabaseAndState?

Django's migration system maintains two parallel layers:

  • Database state — what actually exists in PostgreSQL
  • Migration state — Django's internal record of the schema, built by replaying all migration operations in order

Normally these stay in sync because operations like AlterField do both at once: run the SQL against the database and update Django's tracked state. SeparateDatabaseAndState is an escape hatch that lets you decouple these two layers, providing separate operation lists for each:

migrations.SeparateDatabaseAndState(
    database_operations=[...],  # what actually runs against PostgreSQL
    state_operations=[...],     # what Django uses to update its internal state only
)

The issue

Django's AlterField on a FK or OneToOneField generates ALTER TABLE ... ALTER COLUMN ... TYPE ... in PostgreSQL. PostgreSQL blocks this statement when the column is referenced in a trigger WHEN clause or an RLS policy expression, producing:

cannot alter type of a column used in a trigger definition
cannot alter type of a column used in a policy definition

Since the only actual schema change needed is adding a db_comment, we use COMMENT ON COLUMN instead — a separate DDL statement that does not touch the column type and is not blocked by triggers or policies. However, using a plain RunSQL("COMMENT ON COLUMN ...") would leave Django's migration state out of sync, causing makemigrations to re-detect the missing db_comment and regenerate the same failing AlterField on the next run.

SeparateDatabaseAndState solves both problems:

  • database_operations runs COMMENT ON COLUMN — safe, avoids the conflict
  • state_operations runs AlterField against Django's state only (never touches the DB) — keeps the tracked schema in sync

The affected columns, grouped by the reason they are blocked:

Blocked by pgtrigger trigger WHEN clauses:

Trigger Table Column
restrict_bcghg_id_unless_operation_registered erc.facility bcghg_id_id
restrict_bcghg_id_unless_registered erc.operation bcghg_id_id
restrict_boro_id_unless_registered erc.operation bc_obps_regulated_operation_id

Blocked by RLS policy subquery expressions:

Policy Table Column
compliance_obligation_industry_user_delete_policy and operation policies erc.operation operator_id
Multiple compliance and registration policies erc.user_operator operator_id
Multiple compliance and registration policies erc.user_operator user_id

@Sepehr-Sobhani Sepehr-Sobhani self-assigned this Jun 12, 2026
@Sepehr-Sobhani
Sepehr-Sobhani force-pushed the 1456-registration-app-tables-have-meaningful-metadata branch 2 times, most recently from 6ee2c24 to 89552a4 Compare June 12, 2026 22:41
@Sepehr-Sobhani
Sepehr-Sobhani force-pushed the 1456-registration-app-tables-have-meaningful-metadata branch from 89552a4 to 973c715 Compare June 16, 2026 16:50

@dleard dleard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good! I see a couple places where a URL to the source regulation may be helpful?

Comment thread bc_obps/registration/models/opted_in_operation_detail.py Outdated
Comment thread bc_obps/registration/models/regulated_product.py Outdated
@Sepehr-Sobhani
Sepehr-Sobhani force-pushed the 1456-registration-app-tables-have-meaningful-metadata branch 2 times, most recently from c6b470e to 5784e37 Compare June 19, 2026 23:43
@Sepehr-Sobhani
Sepehr-Sobhani force-pushed the 1456-registration-app-tables-have-meaningful-metadata branch from 48e317a to 9018d46 Compare June 20, 2026 00:16
@Sepehr-Sobhani
Sepehr-Sobhani merged commit 40476d7 into develop Jun 20, 2026
41 checks passed
@Sepehr-Sobhani
Sepehr-Sobhani deleted the 1456-registration-app-tables-have-meaningful-metadata branch June 20, 2026 00:28
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