Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown column 'ModelName.id' in 'field list'"

I am using Django with mysql. I have created models.py using command inspectdb from existing database in mysql. It has a model as below :

class Participationdata(models.Model):
    userid = models.ForeignKey('Volunteer', models.DO_NOTHING, db_column='UserId')  # Field name made lowercase.
    eventid = models.ForeignKey('Events', models.DO_NOTHING, db_column='EventId')  # Field name made lowercase.
    ptrflag = models.IntegerField(db_column='PtrFlag', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'ParticipationData'
        unique_together = (('userid', 'eventid'),)

I have inserted the data into database using this model successfully. But while selecting data it is showing the error like :

django.db.utils.OperationalError: (1054, "Unknown column 'Participationdata.id' in 'field list'")

How should I resolve this ?

like image 932
Rajadip Avatar asked Oct 24 '25 19:10

Rajadip


1 Answers

Usually you would set primary_key = True on your eventid field, so Django uses it as a default id. Look at the answer in Custom id field in Django model

But it is a ForeignKeyField, so you have to add id column to your database and model. You can remove managed = False (default is True) and then:

  1. Create migrations for the model and migrate --fake-initial (since you have table already, see more: https://docs.djangoproject.com/en/1.10/ref/django-admin/#cmdoption-migrate-fake-initial )
  2. Add AutoField id to your model
  3. Run makemigrations and migrate as usual.

That way django uses your already-existing table, but you can modify it via models.

like image 157
Janusz 'Ivellios' Kamieński Avatar answered Oct 27 '25 08:10

Janusz 'Ivellios' Kamieński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!