Re: OperationalError: (1054, "Unknown,column ' ...) for ForeignKey

2008-08-20 Thread Marc Vinyes

Thanks a lot for your help Malcom.

Malcolm Tredinnick escribió, El 18/08/2008 0:03:
> 
> On Sun, 2008-08-17 at 23:51 +0200, MarC wrote:
>> Hi again,
>> I'm still fighting against it and reading related posts in the mailing list 
>> archive with no 
>> success... Now I just found it won't work either for a simpler example.
>> Looks like I should be doing something really stupid but I can't see it...
>>
>> from django.db import models
>>
>> class Test2(models.Model):
>>  name = models.CharField(max_length=50);
>>  def __unicode__(self):
>>  return self.name
>>
>> class Test(models.Model):
>>  name = models.CharField(max_length=50);
>>  rel = models.ForeignKey(Test2);
> 
>>  def __unicode__(self):
>>  return self.name
>>
>> SHELL:
>>  >>> from trustworker.main.models import *
>>  >>> t2=Test2(name="sample");
>>  >>> t=Test(name="example",rel=t2);
>>  >>> t2.save();
>>  >>> t.save();
> 
> This is caused by an implementation detail, but the basic rule is "don't
> use unsaved objects as related objects".
> 
> What's going on is when you do the "rel=t2" part, the related field
> attribute actually extracts the value it will be storing (the primary
> key value of t2, in this case) and stores that. It doesn't store the
> reference to t2 directly on the attribute (it does store the reference
> internally for caching reasons, but that's beside the point here).
> 
> Since t2 is unsaved, it's primary key attribute is None and that's why
> the wheels fall off later. Saving t2 prior to using it to construct t
> will almost certainly fix the problem.
> 
> Yes, this is slightly odd and a little trappy.
> 
> Regards,
> Malcolm
> 
> 
> 
> > 
> 

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: OperationalError 1054 Unknown column

2007-08-10 Thread AnaReis

Hum... I thought that to be an AutoField it was mandatory that "_id"
would have to be added to the name of the field... Apparently not! :)
Thanks.

Ana

On Aug 9, 6:57 pm, Collin Grady <[EMAIL PROTECTED]> wrote:
> Why did you add _id to your model definition for levelID ? The column
> does not have that.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: OperationalError 1054 Unknown column

2007-08-09 Thread Collin Grady

Why did you add _id to your model definition for levelID ? The column
does not have that.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



OperationalError 1054 Unknown column

2007-08-09 Thread AnaReis

Hi all,
I'm working with a MySQL legacy DB which was changed.

This was our previous Level table:
+-+---+--+-+-+---+
| Field   | Type  | Null | Key | Default | Extra |
+-+---+--+-+-+---+
| level_name  | varchar(20)   | NO   | PRI | |   |
| instrument_name | varchar(20)   | NO   | PRI | |   |
| available   | enum('Y','N') | NO   | | Y   |   |
| tablename   | varchar(20)   | YES  | | NULL|   |
+-+---+--+-+-+---+

This was the previous model class for the Level table:

class Level(models.Model):
level_name = models.CharField(primary_key=True, maxlength=60)
instrument_name = models.CharField(blank=False, maxlength=60)
available = models.TextField(blank=False)
tablename = models.CharField(blank=True, maxlength=60)

Because in the database the primary key was made of two fields, the
level_name and the instrument_name this wasn't working properly. So we
changed the table to:

+-+---+--+-+-
++
| Field   | Type  | Null | Key | Default |
Extra  |
+-+---+--+-+-
++
| level_name  | varchar(20)   | NO   | MUL |
||
| instrument_name | varchar(20)   | NO   | |
||
| available   | enum('Y','N') | NO   | | Y
||
| tablename   | varchar(20)   | YES  | | NULL
||
| levelID | bigint(20)| NO   | PRI | NULL|
auto_increment |
+-+---+--+-+-
++

and the Level model class to (with the help of the inspectdb command):

class Level(models.Model):
level_name = models.CharField(unique=True, maxlength=60)
instrument_name = models.CharField(unique=True, maxlength=60)
available = models.TextField()
tablename = models.CharField(blank=True, maxlength=60)
levelID_id = models.AutoField(primary_key=True)

But now, when I try to do for example this:

def view_levels(request, orderby):

levels=Level.objects.all()

levellist=[]
for l in levels:
levellist.append({'levelname':l.level_name,
  'instrname':l.instrument_name,
  'available':l.available,
  'tablename':l.tablename})

ordering='Instrument Name'
return render_to_response('view_levels.html',
{'levellist':levellist, 'ordering':ordering})

It gives me this error:

Exception Type: OperationalError
Exception Value:(1054, "Unknown column 'Level.levelID_id' in 'field
list'")
Exception Location: /nobackup/reis/Python/lib/python2.5/site-packages/
MySQLdb/connections.py in defaulterrorhandler, line 35

What is happening here?
Thanks for your time!

Ana


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---