google-code-export / django-mptt

Automatically exported from code.google.com/p/django-mptt
Other
0 stars 0 forks source link

null=True needed when creating lft, rgt, tree_id and level fields #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by calvin.c...@gtempaccount.com on 13 Sep 2009 at 8:45

GoogleCodeExporter commented 9 years ago
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? 

Original comment by matjaz.c...@gmail.com on 4 Sep 2010 at 1:04

GoogleCodeExporter commented 9 years ago
I get this error too. It happens when trying to load some data from fixtures 
into the tree.

Original comment by Valentin...@gmail.com on 9 Nov 2010 at 10:11

GoogleCodeExporter commented 9 years ago
Valentin

What version of mptt are you using? Can you please supply your models.py and 
fixture?

Original comment by craig.ds@gmail.com on 10 Nov 2010 at 6:38

GoogleCodeExporter commented 9 years ago
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

Original comment by Valentin...@gmail.com on 12 Nov 2010 at 12:33

GoogleCodeExporter commented 9 years ago
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": ""}}]

Original comment by craig.ds@gmail.com on 12 Nov 2010 at 1:11

GoogleCodeExporter commented 9 years ago
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.

Original comment by Valentin...@gmail.com on 12 Nov 2010 at 1:21

GoogleCodeExporter commented 9 years ago
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 :)

Original comment by craig.ds@gmail.com on 12 Nov 2010 at 11:27

GoogleCodeExporter commented 9 years ago
I've hit the same problem. It would be good to set a default so that data can 
still be imported from old fixtures. 

Original comment by i...@madteckhead.com on 1 Nov 2011 at 4:42