(New to Django) – I am looking to create two model with a foreign key. The first model is called Portfolio
, and each Portfolio has many member through the second model Portfoliomember
.
It currently looks like this:
class Portfolio(models.Model): portfolio_name = models.CharField(max_length=30, blank=True, null=True) def __str__(self): return self.portfolio_name class Meta: db_table = "portfolio" class PortfolioMember(models.Model): portfolio = models.ForeignKey(Portfolio, related_name = "portfoliomember", on_delete=models.CASCADE) quality = models.FloatField(blank=True, null=True) def __str__(self): return self.portfolio class Meta: db_table = "portfoliomember"
Later i aim at using formset in a form. but for now, when i try to create the migration, i get the following :
Running migrations: Applying app.0019_auto_20210318_1544...Traceback (most recent call last): File "C:UsersFrankyDoulAppDataLocalProgramsPythonPython37libsite-packagesdjangodbbackendsutils.py", line 85, in _execute return self.cursor.execute(sql, params) psycopg2.errors.UndefinedTable: relation "portfoliomember" does not exist django.db.utils.ProgrammingError: relation "portfoliomember" does not exist
Is there an obvious reason why this would not work ?
Advertisement
Answer
Why do you define in your Meta class db_table? I don’t know what is your app name but normally you don’t need to do that except for specific reason. See this (https://docs.djangoproject.com/en/3.1/ref/models/options/) ” Django automatically derives the name of the database table from the name of your model class and the app that contains it”.
So when you define db_table = ‘another_name’, you override the database table name. I would suggest to remove this line i.e. db_table = “portfoliomember” and see if the error persist.