Home:ALL Converter>How do i change the default primary key in django?

How do i change the default primary key in django?

Ask Time:2018-01-18T11:48:31         Author:john wayne

Json Formatter

I'm new to learning django. I'm using PostgreSQL as my database. I created an app called gplus and a model I created looks something like this

class Account(AbstractBaseUser):
  email = models.EmailField(max_length=50, unique=True)
  username = models.CharField(max_length=50, unique=True)
  first_name = models.CharField(max_length=30)
  last_name = models.CharField(max_length=30)
  date_of_birth = models.DateTimeField()

I applied the migrations and then wanted to change the primary key field from default 'id' to 'username'. So, in the psql command line I ran the following commands

ALTER TABLE gplus_account DROP CONSTRAINT gplus_account_pkey;
ALTER TABLE gplus_account ADD PRIMARY KEY (username);
ALTER TABLE gplus_account DROP COLUMN id;

Then in the models.py, I edited the username field as

username = models.CharField(max_length=50, primary_key=true)

Now, when I try to apply the migrations, I get the error

django.db.utils.ProgrammingError: column "id" of relation "gplus_account" does not exist

It seems Django needs to know there is no 'id' field in the table anymore. How can I do that?

Thanks

Author:john wayne,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48313542/how-do-i-change-the-default-primary-key-in-django
yy