﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34340	Apparently wrong table name?	Nicola Zilio	nobody	"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)

{{{
#!div style=""font-size: 100%""
Code highlighting:
  {{{#!python
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.
"	Bug	new	Database layer (models, ORM)	3.2	Normal				Unreviewed	0	0	0	0	0	0
