0

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

1 Answers1

0

When you run python manage.py showmigrations, are all the boxes checked or is there one or more not checked [x]?

For instance, this would indicate no migrations have been run

(venv) bash-3.2$ python manage.py showmigrations
admin
 [ ] 0001_initial
 [ ] 0002_logentry_remove_auto_add
 [ ] 0003_logentry_add_action_flag_choices
auth
 [ ] 0001_initial
 [ ] 0002_alter_permission_name_max_length
 [ ] 0003_alter_user_email_max_length
 [ ] 0004_alter_user_username_opts
 [ ] 0005_alter_user_last_login_null
 [ ] 0006_require_contenttypes_0002
 [ ] 0007_alter_validators_add_error_messages
 [ ] 0008_alter_user_username_max_length
 [ ] 0009_alter_user_last_name_max_length
 [ ] 0010_alter_group_name_max_length
 [ ] 0011_update_proxy_permissions
contenttypes
 [ ] 0001_initial
 [ ] 0002_remove_content_type_name
sessions
 [ ] 0001_initial

And this is when all migrations have been applied:

(venv) bash-3.2$ python manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [ ] 0001_initial

If you need to target a specific migration that has not yet been applied you can run the following: Lets say sessions needs to be run

python manage.py migrate sessions 0001_initial

This would apply that specific migration to the database. Are you getting additional errors?

ja408
  • 798
  • 5
  • 16
  • It show marks for all. but /election/poll/migrations/0001_initial.py this file still does not have entry for OneToOneField – Vishvajeet Ramanuj Mar 31 '20 at 10:39
  • If you run `python manage.py migrate election 0001_initial` what happens? – ja408 Mar 31 '20 at 15:30
  • It show me this = > CommandError: No installed app with label 'election'. – Vishvajeet Ramanuj Apr 02 '20 at 02:33
  • That usually means that the app `election` isn't present in the `INSTALLED_APPS` section of the `settings.py` file. – ja408 Apr 02 '20 at 07:06
  • my app name is poll so when I entered command `python manage.py migrate poll 0001_initial` it gives output `Operations to perform: Target specific migration: 0001_initial, from poll Running migrations: No migrations to apply. ` – Vishvajeet Ramanuj Apr 03 '20 at 03:49