JavaCake
JavaCake

Reputation: 4115

db_table option does not work in Django model

I want to remove the app label from the table name hence i have a extremely simple web application.

I am using following model:

class Gender(models.Model):
    name = models.CharField(max_length=20)

    class Meta:
        db_table = 'gender'

The result is not quite as expected:

>>> Gender.objects.all()
ProgrammingError: (1146, "Table 'project.gender' doesn't exist")

Why is the meta option not working?

I am using Django 1.6.2

Upvotes: 3

Views: 8266

Answers (2)

A R SHAKIL
A R SHAKIL

Reputation: 31

I got the solution, if you want to change you model name using like this..

class Gender(models.Model):
    name = models.CharField(max_length=20)


    class Meta:
        db_table = 'gender'

after that you have to maigrate your model again

  1. python manage.py makemigrations
  2. python manage.py migrate

Upvotes: 3

Anurag
Anurag

Reputation: 3114

db_table option gives a flexibility to user for specifying a database table name.

From the error it looks like that table gender does not exist in database. That means after specifying this option you haven't performed syncdb. Please do a syncdb or if you are using south then schemamigration.

Upvotes: 4

Related Questions