views:

1775

answers:

3

I'm having an error where I am not sure what caused it.

Here is the error:

Exception Type:     OperationalError
Exception Value:    
(1054, "Unknown column 'user_id' in 'field list'")

Does anyone know why I am getting this error? I can't figure it out. Everything seems to be fine.

My view code is below:

if "login" in request.session:
    t = request.POST.get('title', '')
    d = request.POST.get('description', '')
    fid = request.session["login"]
    fuser = User.objects.get(id=fid)
    i = Idea(user=fuser, title=t, description=d, num_votes=1)
    i.save()
    return HttpResponse("true", mimetype="text/plain")
else:
    return HttpResponse("false", mimetype="text/plain")

I appreciate any help! Thanks!

Edit: Also a side question. Do I use objects.get(id= or objects.get(pk= ? If I use a primary key, do I need to declare an id field or an index in the model?

Edit: Here are the relevant models:

class User (models.Model):
    first_name = models.CharField(max_length=200)
    last_name = models.CharField(max_length=200)
    email = models.CharField(max_length=200)
    password = models.CharField(max_length=200)

class Idea (models.Model):
    user = models.ForeignKey(User)
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=255)
    num_votes = models.IntegerField()
+1  A: 

You'll have to show your models to get real help, but it looks like your Idea table doesn't have a user_id column? Did you modify the SQL table structure?

Ned Batchelder
I did not touch the SQL table structure. I added my model to the question to show it. I ran syncdb multiple times.
rksprst
syncdb does not change table structures, it can only create nonexisting tables. Thus if you change your models, you'll still have old tables, and need to drop tables and recreate them again (or alter them manually).
che
Awesome!! Thank you. I dropped all of the tables and it worked fine.
rksprst
+3  A: 
  1. The user_id field is the FK reference from Idea to User. It looks like you've changed your model, and not updated your database, then you'll have this kind of problem.

    Drop the old table, rerun syncdb.

  2. Your model tables get an id field by default. You can call it id in your queries. You can also use the synonym of pk.

    If you define your own primary key field you, you don't get the automatic id field. But you can still use pk to refer to the Primary Key.

S.Lott
"pk" is not an synonym for ID, it designates primary key column (may me named differently). Very handy if you do not know (or do not want to) the exact name for primary key column.
zgoda
http://docs.djangoproject.com/en/dev/topics/db/queries/#the-pk-lookup-shortcut. PK means the PK, either the one you defined in the model or the default PK, which will be ID.
S.Lott
A: 

Yes, I dropped the tables and it all worked great. However, you have to actually go into the database and DROP them. "manage.py flush" or "manage.py reset appname" won't do it by themselves.

-Nick O

Nick Orlowski