I am changing the primary key of the legacy database. I was able to change the primary key by setting id as the primary key.
Before
class User(models.Model):
name = models.CharField(max_length=5)
email = models.CharField(max_length=5)
age = models.CharField(max_length=5)
After
class User(models.Model):
id = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length=5)
email = models.CharField(max_length=5)
age = models.CharField(max_length=5)
Then
python manage.py makemigrations
python manage.py migrate
This is working fine. But I also want to change the default primary key of the tables created via ManyToMany feild.
User Model
class User(models.Model):
id = models.BigIntegerField(primary_key=True)
name = models.CharField(max_length=5)
email = models.CharField(max_length=5)
age = models.CharField(max_length=5)
UserProfile Model
class UserProfile(models.Model):
id = models.BigIntegerField(primary_key=True)
address = models.CharField(max_length=5)
father_name = models.CharField(max_length=5)
pincode = models.CharField(max_length=5)
user = models.ManyToManyField(User)
The ManytoMany field creates table called User_user_userprofile with id as Autofield basically previous or default django primary key.
id, user_id, userprofile_id ManytoMany Table
Now, How to change the primarykey of ManytoMany Feild ie id created by Django?
PS: Django: 1.11 Python: 2.7.5 DB: Sqlite3 3.7.17 2013-05-20
I stumbled upon this problem today, and ended up solving it by using the
throughargument of theManyToManyField. I solved it for Django v3.2.6 however, but the documentation for v1.11 mentions the same behavior for the same argument, so hopefully the solution should work for your version of Django too. Here's the link to the documentation for v1.11 ManyToManyField.throughWhat the
throughargument allows you to do is to create the intermediary table (created automatically byManyToManyField) yourself. You get finer control of how the intermediary table should look like, what fields it should have and what their behavior should be. Hope you are getting a picture.Let me give you the example of the problem I faced and how I solved it. Hopefully that will make this clearer.
I was trying to establish a many-to-many relationship between two of my existing models.
My first model looks like this,
and the second one looks like,
But this resulted in an intermediary table
wordnet_englishwords_bangla_wordwhich looked like this,But I didn't want this, I wanted
bng_idto be thepkfor this table. I solved the problem withManyToManyField.throughas follows,I defined the intermediary model(table) myself and with the
throughargument, I pointed to the new intermediary model I created and instructed django to create the table the way I wanted it.First I created the intermediary model,
which defines
bng_idas the primary key as I desired.Second, I told the
ManyToManyFieldinEnglishWordsto base the table onBanglaEnglishRelationslike,This resulted in the table
wordnet_banglaenglishrelationswhich looked like,and surved my purposes. You can do something similar to solve your problem and promote whatever field to a
pk.