Reputation: 145
I'm trying to read values from an sqlite db I added to my Django project but it doesn't work. I did a test in the Python shell and all it returned was the following error when I tried looking into the data:
from myapp.models import my_data
my_data.objects.all()
OperationalError: no such column: my_table_name.id
This is how my models.py file looks like:
class my_data(models.Model):
status = models.TextField(db_column='STATUS', blank=True, null=True)
name_1 = models.TextField(db_column='NAME_1', blank=True, null=True)
name_2 = models.TextField(db_column='NAME_2', blank=True, null=True)
dep = models.IntegerField(db_column='DEP', blank=True, null=True)
name_reg = models.TextField(db_column='NAME_REG', blank=True, null=True)
reg = models.IntegerField(db_column='REG', blank=True, null=True)
name_com = models.TextField(db_column='NAME_COM', blank=True, null=True)
avgp = models.IntegerField(db_column='AVGP', blank=True, null=True)
class Meta:
managed = True
db_table = 'my_table_name'
My settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'my_table_name': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db_my_table_name.sqlite3'),
}
}
Also, I performed the python manage.py makemigrations
and python manage.py migrate
commands.
Any idea what I am doing wrong?
Upvotes: 1
Views: 2881
Reputation: 351
I had the same issue before then i used the below code for my models.py file, this has resolved the issue
class ModelName(models.Model):
id = models.AutoField(primary_key=True)
This could help you to resolve the above Problem
Upvotes: 0
Reputation: 886
I write this according to this comment since you said it worked:
Add a id field to my_data
model:
AutoField like: id = models.AutoField(primary_key=True)
Tip: In django model names should follow CamelCase convention.
Upvotes: 2