I have model named Voter. I want to authenticate it using django authentication. So I added OneToOneField. I am using this tutorial.
but when I add bellow line, applyed makemigrations
and migrate
and try to fetch Voter objects then it generate error
user = models.OneToOneField(User, on_delete=models.CASCADE)
Previously I thought that i had done something wrong with extending user. But reading other answers in stackoverflow now it seems that it is because of migration is not applying.
Code model.py(partial)
class Voter(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) # this line map voter with user but it produce error
serial_voter_id = models.AutoField(primary_key=True)
voter_id = models.CharField(unique=True, max_length=10)
voter_name = models.CharField(max_length=255)
voter_constituency = models.ForeignKey(Constituency, models.DO_NOTHING, blank=True, null=True)
username = models.CharField(unique=True, max_length=32)
password = models.TextField()
voter_address = models.CharField(max_length=255, blank=True, null=True)
area = models.CharField(max_length=10, blank=True, null=True)
city = models.CharField(max_length=10, blank=True, null=True)
pincode = models.IntegerField(blank=True, null=True)
adhar_no = models.BigIntegerField(unique=True)
birth_date = models.DateField()
age = models.IntegerField()
fingerprint = models.TextField(blank=True, null=True)
authenticity = models.CharField(max_length=3, blank=True, null=True)
wallet_id = models.TextField()
class Meta:
managed = False
db_table = 'voter'
migration entry from migrations/0001_initial.py
migrations.CreateModel(
name='Voter',
fields=[
('serial_voter_id', models.AutoField(primary_key=True, serialize=False)),
('voter_id', models.CharField(max_length=10, unique=True)),
('voter_name', models.CharField(max_length=255)),
('username', models.CharField(max_length=32, unique=True)),
('password', models.TextField()),
('voter_address', models.CharField(blank=True, max_length=255, null=True)),
('area', models.CharField(blank=True, max_length=10, null=True)),
('city', models.CharField(blank=True, max_length=10, null=True)),
('pincode', models.IntegerField(blank=True, null=True)),
('adhar_no', models.BigIntegerField(unique=True)),
('birth_date', models.DateField()),
('age', models.IntegerField()),
('fingerprint', models.TextField(blank=True, null=True)),
('authenticity', models.CharField(blank=True, max_length=3, null=True)),
('wallet_id', models.TextField()),
],
options={
'db_table': 'voter',
'managed': False,
},
),
the error it generate is
OperationalError at /admin/poll/voter/
(1054, "Unknown column 'voter.user_id' in 'field list'")
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/poll/voter/
Django Version: 3.0.2
Exception Type: OperationalError
Exception Value:
(1054, "Unknown column 'voter.user_id' in 'field list'")
Exception Location: /home/vishvajeet/Desktop/Programming/django/environment/django/lib/python3.6/site-packages/MySQLdb/connections.py in query, line 239
Python Executable: /home/vishvajeet/Desktop/Programming/django/environment/django/bin/python
Python Version: 3.6.9
Python Path:
['/home/vishvajeet/Desktop/Programming/django/environment/election',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/vishvajeet/Desktop/Programming/django/environment/django/lib/python3.6/site-packages']
Server time: Tue, 31 Mar 2020 05:03:53 +0000
I seach for voter.user_id but did't found it any file. I read this answer and think that it is because of migration is not applyting and also we can see that entry of OneToOneField is not in migration file.
I am useing Django==3.0.2 mysqlclient==1.4.6
I made database in mysql and then used it in django using this