Django automatically create primary keys for existing database tables

2024/10/12 15:29:54

I have an existing database that I'm trying to access with Django. I used python manage.py inspectdb to create the models for the database. Currently I'm able to import the models into the python shell however when I try to access any of the actual objects in any way, I get this error OperationalError: (1054, "Unknown column 'some_table.id' in 'field list'"). I see that the table in the database in fact does not have an id field. How can I fix this? Do I need to update the managed field in the Meta class and run a migration so it can create this field automatically?

Answer

From the Django documentation: This feature is meant as a shortcut, not as definitive model generation. See the documentation of inspectdb for more information. (Reference: https://docs.djangoproject.com/en/1.8/howto/legacy-databases/)

You're going to need to manually clean up the models and migrate. The line you'll have to add for adding the "id" field is:

id = models.IntegerField(primary_key=True)

Warning: I'd definitely create a copy of the database to toy with, rather than the original. This will likely take you some trial and error to get right. After you're absolutely sure you have it right, you can changed Managed=True, but be VERY careful!

https://en.xdnf.cn/q/69638.html

Related Q&A

matplotlib.pyplot scatterplot legend from color dictionary

Im trying to make a legend with my D_id_color dictionary for my scatterplot. How can I create a legend based on these values with the actual color? #!/usr/bin/python import matplotlib.pyplot as plt f…

Numpy Array Set Difference [duplicate]

This question already has answers here:Find the set difference between two large arrays (matrices) in Python(3 answers)Closed 7 years ago.I have two numpy arrays that have overlapping rows:import numpy…

Pylint not working within Spyder

Ive installed Anaconda on a Windows computer and Spyder works fine, but running pylint through the Static Code Analysis feature gives an error. Pylint was installed through Conda. Note: Error in Spyder…

WTForms doesnt validate - no errors

I got a strange problem with the WTForms library. For tests I created a form with a single field:class ArticleForm(Form):content = TextField(Content)It receives a simple string as content and now I use…

NameError: global name numpy is not defined

I am trying to write a feature extractor by gathering essentias (a MIR library) functions. The flow chart is like: individual feature extraction, pool, PoolAggregator, concatenate to form the whole fea…

Django exclude from annotation count

I have following application:from django.db import modelsclass Worker(models.Model):name = models.CharField(max_length=60)def __str__(self):return self.nameclass Job(models.Model):worker = models.Forei…

How to use yield function in python

SyntaxError: yield outside function>>> for x in range(10): ... yield x*x ... File "<stdin>", line 2 SyntaxError: yield outside functionwhat should I do? when I try to use …

Tkinter looks different on different computers

My tkinter window looks very different on different computers (running on the same resolution!):windows 8windows 7I want it to look like it does in the first one. Any ideas?My code looks like this:cla…

Sort when values are None or empty strings python

I have a list with dictionaries in which I sort them on different values. Im doing it with these lines of code:def orderBy(self, col, dir, objlist):if dir == asc:sorted_objects = sorted(objlist, key=la…

How to tell if you have multiple Djangos installed

In the process of trying to install django, I had a series of failures. I followed many different tutorials online and ended up trying to install it several times. I think I may have installed it twice…