user3423787
user3423787

Reputation: 95

django.db.utils.OperationalError: my_table has no column id error?

I'm testing my models.py file of my Django app: "myapp" in my Python interepreter, but keep running into this error:

django.db.utils.OperationalError: table myapp_table1 has no column named attribute1_id

I have 2 tables in my models.py, table1 and table 2.

 class table1 (models.Model):
   name = models.CharField(max_length=25)
   def __str__(self):             
     return self.name
 class table2 (models.Model):
   name = models.CharField(max_length =25)
   attribute1= models.ForeignKey(Type, related_name = "att1_set+", default=None)
   attribute2= models.ManyToManyField(Type, related_name = "att2_set+", default=None)
   def __str__(self):             
     return self.metric_name

In my python interpreter:

>>> from my_app.models import table1, table2
>>> t1 = table1 (name = "ty1", id = 0)
>>> t1.save()
>>> t2 = table1 (name = "ty2", id = 1)
>>> e1 = table2 (name = "as", attribute1 = t1)
>>> e1.save() 
>>> error aforementioned after .save

However when I exe: "manage.py sql myapp", I see for my table2:

CREATE TABLE "my_app_table2" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(25) NOT NULL,
"attribute1_id" integer NOT NULL REFERENCES "myapp_table1" ("id")
)

Upvotes: 9

Views: 18909

Answers (2)

Ruslan Osipov
Ruslan Osipov

Reputation: 5843

You have to run python manage.py syncdb (or python manage.py migrate if you use south - which you should) in order for changes to be applied to the database.

Edit: As mentioned here, as of Django version 1.9 and onwards the syncdb command was removed; you now use python manage.py makemigrations and then python manage.py migrate.

Upvotes: 6

Kang Das
Kang Das

Reputation: 11

first delete pycache, and all 0001_initial.py, then python manage.py flush, then

python manage.py makemigrations

then

python manage.py migrate

then

python manage.py createsuperuser

Upvotes: 0

Related Questions