Skip to content
Advertisement

Double quotes problems with Postgres

I am using Django with postgres. Here’s the snippet of my code which I am using to create tables in the db.

class user(models.Model):
    user_id = models.IntegerField(primary_key=True)
    username = models.CharField(max_length=30)
    password = models.CharField(max_length=30)
    role_id = models.ForeignKey(role,on_delete=models.CASCADE)
    class Meta:
        db_table = 'user'

My question is:

  1. why is the user table getting renamed as “user” (with quotes)
  2. why is the role_id getting named as role_id_id where as I have clearly mentioned role_id to be my column name? enter image description here

Advertisement

Answer

After reading few answers on Stackoverflow, I think the reason why instead of user (without quotes) a “user” table was getting created is because user is a reserved keyword in postgres and cannot be used for the table name. Coming to the second question about the _id part:

Behind the scenes, Django appends "_id" to the field name to create
its database column name.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement