#34340 closed Bug (needsinfo)

Apparently wrong table name?

Reported by: Nicola Zilio Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
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 Nicola Zilio)

I think I may have found a bug in how Django automatically creates table names, as I cannot find anything in my code that can explain the behaviour I observe.

Let's say that I have the following, related, models in an app called collection (see https://github.com/helle-ulrich-lab/lab-database/blob/a5ccfc62bc1257e8640eb9fe235732b34408f989/collection/models.py#L67 for actual code)

Code highlighting:

class SaCerevisiaeStrain (models.Model, SaveWithoutHistoricalRecord):
    name = models.CharField("name", max_length=255, unique=True, blank=False)
    episomal_plasmids = models.ManyToManyField('Plasmid', related_name='cerevisiae_episomal_plasmids', blank=True, through='SaCerevisiaeStrainEpisomalPlasmid')

class Plasmid (models.Model, SaveWithoutHistoricalRecord):
    name = models.CharField("name", max_length=255, unique=True, blank=False)

class FormZProject (models.Model):
    title = models.CharField("title", max_length=255, blank=False)

class SaCerevisiaeStrainEpisomalPlasmid (models.Model):
    sacerevisiae_strain = models.ForeignKey(SaCerevisiaeStrain, on_delete=models.PROTECT)
    plasmid = models.ForeignKey('Plasmid', verbose_name = 'Plasmid', on_delete=models.PROTECT)
    formz_projects = models.ManyToManyField(FormZProject, related_name='cerevisiae_episomal_plasmid_projects', blank=True)

If I create a new instance of my Django project, with a brand-new Postgres database, and even if I reset all migrations so that the only ones that I have are 000x_initial.py, the DB table for the formz_projects field in SaCerevisiaeStrainEpisomalPlasmid is always created with the following name

collection_sacerevisiaestrainepisomalplasmid_for4259

I have another equivalent set up for a model called ScPombeStrain, instead of SaCerevisiaeStrain, and for that the DB table is always called

collection_scpombestrainepisomalplasmid_formz_pr0c0e

This is a problem because Django expects those tables to be called collection_sacerevisiaestrainepisomalplasmid_formz_projects and collection_scpombestrainepisomalplasmid_formz_projects, and unless I rename them manually in Postgres, it raises an exception. The interesting behaviour is that the names of the tables are always those above and not just some random series of trailing characters. Also, they are both 52 characters long.

Change History (2)

comment:1 by Nicola Zilio, 19 months ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 19 months ago

Resolution: needsinfo
Status: newclosed

Thanks for the ticket, however I cannot reproduce this issue in Django 3.2+ in all cases the table is called collection_sacerevisiaestrainepisomalplasmid_formz_projects as expected. Please reopen the ticket if you can debug your issue and provide a small project that reproduces it.

Note: See TracTickets for help on using tickets.
Back to Top