| Issue 62: | null=True needed when creating lft, rgt, tree_id and level fields | |
| 5 people starred this issue and may be notified of changes. | Back to list |
In mptt/__init__.py file, line 47:
# Add tree fields if they do not exist
for attr in [left_attr, right_attr, tree_id_attr, level_attr]:
try:
opts.get_field(attr)
except FieldDoesNotExist:
PositiveIntegerField(
db_index=True, editable=False,
null=True).contribute_to_class(model, attr)
I am proposing to add in "null=True". Without this, I am unable to add in
a new object into a Class that is registered to use mptt. I will get an
error which says:
null value in column "lft" violates not-null constraint
whenever I attempt to add a new object into my mptt-controlled Class.
regards,
Calvin
Sep 3, 2010
#1
matjaz.c...@gmail.com
Nov 9, 2010
I get this error too. It happens when trying to load some data from fixtures into the tree.
Nov 9, 2010
Valentin What version of mptt are you using? Can you please supply your models.py and fixture?
Status:
Chatting
Owner: craig.ds
Nov 12, 2010
mptt is installed with easy_install: django_mptt-0.4.2-py2.6.egg
Model:
class Category (MPTTModel):
name = models.CharField(_('name'), max_length = 128)
parent = models.ForeignKey('self', null = True, blank = True)
icon = models.ImageField(_('icon'), upload_to = 'icons', blank = True, null = True)
slug = models.SlugField(_('slug'))
def __unicode__(self):
return self.name
class Meta:
verbose_name = _('category')
verbose_name_plural = _('categories')
Fixture:
- model: my_app.category
pk: 1
fields:
name: Category1
Nov 12, 2010
That fixture certainly looks quite broken - it doesn't have any of the MPTT fields, or parent, icon or slug. slug is not-null.
If that's really the fixture you've got, I suspect it wasn't generated properly, unrelated to django-mptt ?
I tried dumping a fixture from that test model, and it ends up like this (as I expected):
python manage.py dumpdata --format=json testapp
[{"pk": 1, "model": "testapp.category", "fields": {"rght": 2, "name": "foo", "parent": null, "level": 0, "lft": 1, "tree_id": 1, "slug": "", "icon": ""}}]
Nov 12, 2010
Sorry, I've added the slug field later than createed this fixture. But adding it changes nothing, I get the error IntegrityError: null value in column "lft" violates not-null constraint anyway. Parent is not needed here because it is a root node. And writing lft, level and rght in fixture seems wrong, as they must be calculated automatically, not by hand.
Nov 12, 2010
You can't load an old fixture with missing fields. So of course you will get that error, since there is no lft in your fixture. If you must make fixtures by hand, you'll have to add the MPTT fields as well. I don't see any sane way around that. Adding null=True to these fields would just mean possible integrity errors, since it makes no sense to have an MPTT model with no MPTT values defined. Of course, the easier and less error prone way to make a fixture would be to create the data you want on a dev machine, and then dump it using manage.py dumpdata :)
Status:
Invalid
Nov 1, 2011
I've hit the same problem. It would be good to set a default so that data can still be imported from old fixtures. |