google-code-export / django-mptt

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

save() fails when explicitly setting id on new row #17

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Using the following model:

from django.db import models
import mptt

class Category(models.Model):
  name = models.CharField(max_length=100)
  parent = models.ForeignKey('self', null=True)
mptt.register(Category)

2. Run:
Category(id=1000, name='foo').save() # fails
Category(name='bar').save() # succeeds

What is the expected output? What do you see instead?
Expected to save normally

What version of the product are you using? On what operating system?
django-mptt revision 104
django-newforms-admin revision 7346 OR django (trunk) r. 7346

Please provide any additional information below.
This seems to be happening because the pre_save hook is checking for the
existence of the pk attribute as a way of checking whether this is an
update. 'pk' will be set in the case that an id is explicitly provided,
which causes the parent lookup to fail.

Original issue reported on code.google.com by david...@gmail.com on 21 Mar 2008 at 6:16

GoogleCodeExporter commented 9 years ago
By the way, I'm currently able to work around the problem by doing a two-step 
save, i.e.:
>>> category.save(raw=True)
>>> category.save()

Original comment by david...@gmail.com on 21 Mar 2008 at 6:26

GoogleCodeExporter commented 9 years ago
I'm trying to do your workaround, but I get this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/tommycli/Development/pyImpulsestorm/djKeepword/durustodj.py", line 73,
in main
    recurseProcessWork(rootwork, notebook)
  File "/home/tommycli/Development/pyImpulsestorm/djKeepword/durustodj.py", line 10,
in recurseProcessWork
    processWork(work, notebook)
  File "/home/tommycli/Development/pyImpulsestorm/djKeepword/durustodj.py", line 24,
in processWork
    djWork.save(raw=True)
  File "/usr/lib/python2.5/site-packages/django/db/models/base.py", line 264, in save
    ','.join(placeholders)), db_values)
  File "/usr/lib/python2.5/site-packages/django/db/backends/util.py", line 18, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.5/site-packages/django/db/backends/sqlite3/base.py", line
133, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: notebooks_work.lft may not be NULL

Any ideas?

Original comment by tommy...@ucla.edu on 23 Mar 2008 at 9:03

GoogleCodeExporter commented 9 years ago
Sorry, I forgot a line:
>>> category.lft = category.rght = category.tree_id = category.level = 0
>>> category.save(raw=True)
>>> category.save()

Original comment by david...@gmail.com on 23 Mar 2008 at 7:10

GoogleCodeExporter commented 9 years ago
I tried the workaround but it doesn't work for me. It saves the instance without
errors alright, but the tree does not contain any useful mptt info after the 
save, so
things like is_leaf_node() no longer work.

A quick hack in the source 'fixed' the problem (normal save() works with this):

# mptt.signals.py
# line 102: (extra check for existence of instance with given pk)
if not instance.pk or not 
instance._default_manager.filter(pk=instance.pk).count():

# mptt.managers.py
# comment out line 120 and 121 (this check breaks when creating with custom pk)
#if node.pk:
#    raise ValueError(_('Cannot insert a node which has already been saved.'))

I'm sure there are better solutions, but it works for me and I don't update the 
tree
very much after an initial import of legacy data (_with_ pk values that need to 
be
maintained)

Maybe this will help the developers in pinpointing and fixing this bug in the 
near
future (hint hint), because this is a great application!

Original comment by rvdri...@gmail.com on 26 Mar 2008 at 8:35

GoogleCodeExporter commented 9 years ago
Yes, allow me to also put in a vote in favor of this application being 
terrific. Keep
it coming!

Original comment by tommy...@ucla.edu on 14 Apr 2008 at 3:07

GoogleCodeExporter commented 9 years ago
Issue 49 has been merged into this issue.

Original comment by jonathan.buchanan on 3 Jul 2009 at 10:44

GoogleCodeExporter commented 9 years ago

Original comment by craig.ds@gmail.com on 13 Nov 2010 at 9:35

GoogleCodeExporter commented 9 years ago
Fixed in master: 
https://github.com/django-mptt/django-mptt/compare/216d62a049b439cc7b01a82cb908f
d838dc9528e...master

Original comment by craig.ds@gmail.com on 13 Nov 2010 at 10:40