Use an explicit m2m through model between Template and Site#155
Closed
blag wants to merge 1 commit intojazzband:masterfrom
Closed
Use an explicit m2m through model between Template and Site#155blag wants to merge 1 commit intojazzband:masterfrom
blag wants to merge 1 commit intojazzband:masterfrom
Conversation
Contributor
Author
|
Two more potential solutions:
Option 3 still relies on database triggers to keep the materialized view up to date, and Django also doesn't support defining or tracking database views in its ORM. Additionally, while MySQL supports views in the most half-hearted way possible (they are essentially saved queries), it does not support adding any sort of unique constraints to views at all. So this solution has zero support from Django's ORM, requires triggers to implement, and still isn't portable across different DBMSes. Option 4 is also not supported by Django's ORM and is obviously not portable across different DBMSes. |
This was referenced May 29, 2025
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.
Create an explicit through model between
TemplateandSiteobjects, instead of relying on the implicit model Django creates for us.There are a lot of guides for doing this that suggest creating the explicit through model, then using
RunPythonin the migration to copy the data from the implicit through table to the explicit table, and removing the old through table. That is problematic for existing users with large amounts of data. So I ensured that this migration doesn't actually modify the database schema at all - it only modifies the migration state as tracked by the Django migration framework.I then attempted to add a unique together constraint between
Template.nameandSite. Django's system check system identified an issue with this:After quote a bit of searching around, I have been unable to find anything to suggest that PostgreSQL in particular (because that's what I'm using) supports explicit table joins in unique together constraints. It seems like databases, including PostgreSQL, only allow unique together constraints on columns that exist in the same table. Every source I found indicated that it could be done in one of two ways:
Option 1 is problematic because while composite primary keys are technically supported by Django, support for them is limited. And changing the template table to use composite primary keys would be a breaking change for ALL existing users of this app. Frankly, I found this solution to be overcomplicated to begin with, and not very well supported by Django.
Option 2 is perfectly reasonable, but Django itself does not define or track triggers in its ORM, so we would need to do this via migrations by writing SQL ourselves. I don't think trigger definitions are very portable between different DBMSes, and in either case, I thought this was even more overcomplicated than option 1 on top of being difficult to troubleshoot , test, and maintain.
But if it somehow is possible to support unique together constraints/indexes that references a joined table field that is not a simple foreign key to the joined table, this is at least a good start in that direction.
Closes #119, #147, and #152.
@mpasternak You seem convinced that this is possible, do you mind picking this up and getting it to work? Maybe I'm missing something.