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:
- why is the user table getting renamed as “user” (with quotes)
- why is the role_id getting named as role_id_id where as I have clearly mentioned role_id to be my column name?
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.