Re: Optional foreign key with django?
This solved my problem On 9 sep, 12:18, Carlton Gibsonwrote: > On 9 Sep 2010, at 12:04, bagheera wrote: > > >> On Sep 8, 11:14 pm, maroxe wrote: > >>> Hi, In my models I want to have an optional field to a foreign key. I > >>> tried this: > > >>> field = models.ForeignKey(MyModel, null=True, blank=True, > >>> default=None) > > >>> But i am getting this error: > > >>> model.mymodel_id may not be NULL > > I think the error is that SQLite treats columns with defaults as NOT NULL. > > As far as I can see, you don't need the `default=None` in your model > declaration. Change it to: > > field = models.ForeignKey(MyModel, null=True, blank=True) > > -- I have multiple apps using SQLite that do exactly this, so to quote an > earlier post, "this should just work (TM)" > > HTH > > Regards, > Carlton -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: Optional foreign key with django?
On 9 Sep 2010, at 12:04, bagheera wrote: >> On Sep 8, 11:14 pm, maroxewrote: >>> Hi, In my models I want to have an optional field to a foreign key. I >>> tried this: >>> >>> field = models.ForeignKey(MyModel, null=True, blank=True, >>> default=None) >>> >>> But i am getting this error: >>> >>> model.mymodel_id may not be NULL I think the error is that SQLite treats columns with defaults as NOT NULL. As far as I can see, you don't need the `default=None` in your model declaration. Change it to: field = models.ForeignKey(MyModel, null=True, blank=True) -- I have multiple apps using SQLite that do exactly this, so to quote an earlier post, "this should just work (TM)" HTH Regards, Carlton -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: Optional foreign key with django?
Dnia 09-09-2010 o 11:00:29 akaariainapisał(a): On Sep 8, 11:14 pm, maroxe wrote: Hi, In my models I want to have an optional field to a foreign key. I tried this: field = models.ForeignKey(MyModel, null=True, blank=True, default=None) But i am getting this error: model.mymodel_id may not be NULL i am using sqlite edit: if it can help, here is the exception location: /usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 200 so it's sqlite specific problem, i think. PS: i droped the whole table and synced before getting this error. This should just work (TM). Are you sure the table is dropped and recreated correctly? The error is from the database (not from Django code), and the reason is a not null constraint on models.mymodel_id which should not exist. Maybe you could try this in completely clean database? - Anssi I have same problem with sqlite. Try another database. Django devs suggest postgres. -- Linux user -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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: Optional foreign key with django?
On Sep 8, 11:14 pm, maroxewrote: > Hi, In my models I want to have an optional field to a foreign key. I > tried this: > > field = models.ForeignKey(MyModel, null=True, blank=True, > default=None) > > But i am getting this error: > > model.mymodel_id may not be NULL > > i am using sqlite edit: if it can help, here is the exception > location: > > /usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py > in execute, line 200 > > so it's sqlite specific problem, i think. > > PS: i droped the whole table and synced before getting this error. This should just work (TM). Are you sure the table is dropped and recreated correctly? The error is from the database (not from Django code), and the reason is a not null constraint on models.mymodel_id which should not exist. Maybe you could try this in completely clean database? - Anssi -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.