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 17: save() fails when explicitly setting id on new row
8 people starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  craig.ds@gmail.com
Closed:  Nov 2010


 
Reported by david...@gmail.com, Mar 21, 2008
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.
 
Mar 21, 2008
#1 david...@gmail.com
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()


Mar 23, 2008
#2 tommy...@ucla.edu
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?
Mar 23, 2008
#3 david...@gmail.com
Sorry, I forgot a line:
>>> category.lft = category.rght = category.tree_id = category.level = 0
>>> category.save(raw=True)
>>> category.save()
Mar 26, 2008
#4 rvdri...@gmail.com
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!
Apr 13, 2008
#5 tommy...@ucla.edu
Yes, allow me to also put in a vote in favor of this application being terrific. Keep
it coming!
Jul 3, 2009
Project Member #6 jonathan.buchanan
 Issue 49  has been merged into this issue.
Nov 13, 2010
Project Member #7 craig.ds@gmail.com
(No comment was entered for this change.)
Status: Accepted
Owner: craig.ds
Labels: Milestone-Release0.5

Powered by Google Project Hosting