My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
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
Status:  Invalid
Owner:  craig.ds@gmail.com
Closed:  Nov 2010


 
Reported by calvin.c...@gtempaccount.com, Sep 13, 2009
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
Looks like this is quite a specific error that I cannot seem to reproduce or fully understand. 
Generally, this attributes need to be present for a complete and efficient tree.
Are you still encountering the error? 
Nov 9, 2010
#2 Valentin...@gmail.com
I get this error too. It happens when trying to load some data from fixtures into the tree.
Nov 9, 2010
Project Member #3 craig.ds@gmail.com
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
#4 Valentin...@gmail.com
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
Project Member #5 craig.ds@gmail.com
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
#6 Valentin...@gmail.com
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
Project Member #7 craig.ds@gmail.com
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
#8 i...@madteckhead.com
I've hit the same problem. It would be good to set a default so that data can still be imported from old fixtures. 

Powered by Google Project Hosting