Re: Can't create a record with a Foreign Key: IntegrityError: (1048, "Column 'user_id' cannot be null")

2009-02-03 Thread Malcolm Tredinnick

On Tue, 2009-02-03 at 08:08 -0800, Theme Park Photo, LLC wrote:
> 
> Ok! NOW I get it! You need to do a get to do a select by primary key
> 
> 
>  u=User.objects.get(username="swirsky")
> 
> 
> For some reason I thought that you could get by primary key by just
> creating the object with the key specified...

Your first version of the code was creating a Python instance. No
interaction with the database. Your new version specifically asks the
database for information.

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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can't create a record with a Foreign Key: IntegrityError: (1048, "Column 'user_id' cannot be null")

2009-02-03 Thread Theme Park Photo, LLC


Ok! NOW I get it! You need to do a get to do a select by primary key


 u=User.objects.get(username="swirsky")


For some reason I thought that you could get by primary key by just
creating the object with the key specified...
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can't create a record with a Foreign Key: IntegrityError: (1048, "Column 'user_id' cannot be null")

2009-02-03 Thread Theme Park Photo, LLC

Thanks! That was a little unintuitive because, of course, I was only
trying to do a "SELECT" on u and an INSERT on R. Save seems like it
would do something to u



On Feb 2, 10:39 pm, Malcolm Tredinnick 
wrote:
> On Mon, 2009-02-02 at 22:22 -0800, Theme Park Photo, LLC wrote:
> > I can't get anything with Foreign keys to work. I trimmed everything
> > down to the simplest example:
>
> > from django.db import models
> > from django.contrib.auth.models import User
>
> > class Ranking(models.Model):
> >    user = models.ForeignKey(User)
> >    score = models.IntegerField()
> >    def __unicode__(self):
> >            return u'%s %d' % (self.user, self.score)
>
> > I can't add any "Ranking" records. I try like this (from the django
> > shell) and always get an error like this:
> > IntegrityError: (1048, "Column 'user_id' cannot be null")
>
> > Can someone give me a hint as to the right way to do this?
>
> > (Django shell session below)
>
> > >>> from django.db import models
> > >>> from django.contrib.auth.models import User
> > >>> from djbaytzim.bayscore.models import Ranking
> > >>> u = User (username="swirsky")
> > >>> print type(u)
> > 
>
> You have to save the User object before you can use it anywhere. At this
> point in time, it doesn't have a primary key value, which is what the
> Ranking object needs to refer to it. Actually, the pk value is None,
> which 'r' then uses, which causes problems later.
>
> Another way of thinking of this: right now, 'u' does not exist in the
> database. So you cannot save 'r' to the database, since it needs to
> refer to another row in the database (the one for 'u', which doesn't
> exist) for the User object.
>
> Call u.save() here (or use User.objects.create(username="swirsky")) and
> everything will work.
>
> 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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Can't create a record with a Foreign Key: IntegrityError: (1048, "Column 'user_id' cannot be null")

2009-02-02 Thread Malcolm Tredinnick

On Mon, 2009-02-02 at 22:22 -0800, Theme Park Photo, LLC wrote:
> I can't get anything with Foreign keys to work. I trimmed everything
> down to the simplest example:
> 
> from django.db import models
> from django.contrib.auth.models import User
> 
> class Ranking(models.Model):
>   user = models.ForeignKey(User)
>   score = models.IntegerField()
>   def __unicode__(self):
>   return u'%s %d' % (self.user, self.score)
> 
> I can't add any "Ranking" records. I try like this (from the django
> shell) and always get an error like this:
> IntegrityError: (1048, "Column 'user_id' cannot be null")
> 
> Can someone give me a hint as to the right way to do this?
> 
> (Django shell session below)
> 
> >>> from django.db import models
> >>> from django.contrib.auth.models import User
> >>> from djbaytzim.bayscore.models import Ranking
> >>> u = User (username="swirsky")
> >>> print type(u)
> 

You have to save the User object before you can use it anywhere. At this
point in time, it doesn't have a primary key value, which is what the
Ranking object needs to refer to it. Actually, the pk value is None,
which 'r' then uses, which causes problems later.

Another way of thinking of this: right now, 'u' does not exist in the
database. So you cannot save 'r' to the database, since it needs to
refer to another row in the database (the one for 'u', which doesn't
exist) for the User object.

Call u.save() here (or use User.objects.create(username="swirsky")) and
everything will work.

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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---