#36802 closed Uncategorized (duplicate)
ManyToManyField Table name changed (django 5.2 > django 6.0)
| Reported by: | ericmuijs | Owned by: | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Dear Django community.
Just upgraded to django 6. Most things seem to work fine. However, I get an error when loading a fixture (during my tests) which has a many to many relationship. The error is, table does not exists. The table does exists, but somehow gets a different name in the (test database) compared to my dev database. The difference in name is related to plural name vs normal name.
Naming of the many to many table:
- Dev Database (tables created by django 5.2) : base_vergunningproductgroep_producten (plural name of related model)
- Test database (tables created by django 6) : base_vergunningproductgroep_product (just the name of the related
No code is changed, except for the upgrade to django 6.
# Removed some unrelated fields for clarity
class VergunningProductGroep(TenantModel):
vergunning = models.ForeignKey(Vergunning, on_delete=models.CASCADE, related_name='vergunningproducten')
producten = models.ManyToManyField(Product, related_name='vergunningproductgroepen')
class Product(TenantModel):
product_code = models.CharField(max_length=50)
omschrijving = models.CharField(max_length=50)
class Meta:
verbose_name = "product"
verbose_name_plural = "producten"
unique_together = ('tenant', 'product_code')
ordering = ['tenant','product_code']
indexes = [
models.Index(fields=['tenant', 'product_code'])
]
Solution tried
I tried to hardcode the db_table using:
class VergunningProductGroep(TenantModel):
vergunning = models.ForeignKey(Vergunning, on_delete=models.CASCADE, related_name='vergunningproducten')
producten = models.ManyToManyField(Product, related_name='vergunningproductgroepen', db_table='vergunningproductgroep_producten')
After running "makemigrations" I expect the issue to be resolved, but to no avail. db_table seems to be ignored completely.
I already reported it https://code.djangoproject.com/ticket/36800, but based on my limited knowledge, it does seem unrelated to renaming? Sorry if this tends to be a duplicate.
Change History (5)
comment:1 by , 13 hours ago
| Description: | modified (diff) |
|---|
comment:2 by , 13 hours ago
| Description: | modified (diff) |
|---|
comment:3 by , 13 hours ago
| Description: | modified (diff) |
|---|
comment:4 by , 13 hours ago
| Description: | modified (diff) |
|---|
comment:5 by , 12 hours ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Duplicate of #36800. You can verify if the proposed patch works for you.