uralbash / sqlalchemy_mptt

SQLAlchemy nested sets mixin (MPTT)
http://sqlalchemy-mptt.readthedocs.io
MIT License
196 stars 32 forks source link

IntegrityError on attempt to initalize the tree #57

Closed sthenault closed 6 years ago

sthenault commented 6 years ago

I'm using sqlalchemy-mptt 0.2.2 and I can't get the sample code to initially create a tree working.

The (almost unmodified from the doc) excerpt:

tree_manager.register_events(remove=True)  # Disable MPTT events
for node in nodes:
    session.add(node)
session.commit()

Node.rebuild(session)  # rebuild lft, rgt value automatically
tree_manager.register_events()  # enabled MPTT events back

is raising an IntegrityError complaining that 'lft' is NULL while it's not authorized on the first commit(). I attempted:

tree_manager.register_events(remove=True)  # Disable MPTT events
for node in nodes:
    node.lft = node.rgt = node.level = node.tree_id = 0
    session.add(node)
session.commit()

Node.rebuild(session  # rebuild lft, rgt value automatically
tree_manager.register_events()  # enabled MPTT events back

but that didn't made the trick either. Everything works fine if I keep events though:

for node in nodes:
    session.add(node)
session.commit()

Am I doing something wrong or is the doc outdated?

BTW, I'm not sure to understand the purpose of the 'tree_id' column. I supposed all nodes below the same root should have the same tree_id, but this doesn't seem to be the case. That may be another story though.

sthenault commented 6 years ago

about the tree_id problem, it appears to be because my node's identifier start from 0, which is considered false in mptt_before_insert and trigger generation of a new tree id. I may submit a patch about this if you're interested.

uralbash commented 6 years ago

@sthenault sorry for the late reply, feel free to add your path it will be great! Thanks!

sthenault commented 6 years ago

done, as well as another one providing additional test case for this issue.

Le mer. 20 juin 2018 à 06:52, uralbash notifications@github.com a écrit :

@sthenault https://github.com/sthenault sorry for the late reply, feel free to add your path it will be great! Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/uralbash/sqlalchemy_mptt/issues/57#issuecomment-398623589, or mute the thread https://github.com/notifications/unsubscribe-auth/ABxJDO0IpF-PRInF8dhDn7LYLxlOAKu_ks5t-dTygaJpZM4UEiIp .

-- Sylvain

uralbash commented 6 years ago

@sthenault I fixed the documentation and tests for rebuild tree, check it plz

http://sqlalchemy-mptt.readthedocs.io/initialize.html#filling-data-at-the-first-time https://github.com/uralbash/sqlalchemy_mptt/blob/master/sqlalchemy_mptt/tests/test_events.py#L196

sthenault commented 6 years ago

great. I'll take a loot at that asap.

Thanks !

Le ven. 22 juin 2018 à 09:11, uralbash notifications@github.com a écrit :

@sthenault https://github.com/sthenault I fixed the documentation and tests for rebuild tree, check it plz

http://sqlalchemy-mptt.readthedocs.io/initialize.html#filling-data-at-the-first-time

https://github.com/uralbash/sqlalchemy_mptt/blob/master/sqlalchemy_mptt/tests/test_events.py#L196

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/uralbash/sqlalchemy_mptt/issues/57#issuecomment-399344584, or mute the thread https://github.com/notifications/unsubscribe-auth/ABxJDEWe-7ghpyJssrGfecqL619YBb-Xks5t_JiUgaJpZM4UEiIp .

-- Sylvain

sthenault commented 6 years ago

seem to work fine, thanks!

Do you have a release plan?

Le ven. 22 juin 2018 à 09:13, Sylvain Thénault thenault@gmail.com a écrit :

great. I'll take a loot at that asap.

Thanks !

Le ven. 22 juin 2018 à 09:11, uralbash notifications@github.com a écrit :

@sthenault https://github.com/sthenault I fixed the documentation and tests for rebuild tree, check it plz

http://sqlalchemy-mptt.readthedocs.io/initialize.html#filling-data-at-the-first-time

https://github.com/uralbash/sqlalchemy_mptt/blob/master/sqlalchemy_mptt/tests/test_events.py#L196

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/uralbash/sqlalchemy_mptt/issues/57#issuecomment-399344584, or mute the thread https://github.com/notifications/unsubscribe-auth/ABxJDEWe-7ghpyJssrGfecqL619YBb-Xks5t_JiUgaJpZM4UEiIp .

-- Sylvain

-- Sylvain

uralbash commented 6 years ago

Nice, I'll do it in the coming days.

uralbash commented 6 years ago

@sthenault done!

sthenault commented 6 years ago

thank you for your reactivity!

Le mar. 3 juil. 2018 à 09:16, uralbash notifications@github.com a écrit :

Closed #57 https://github.com/uralbash/sqlalchemy_mptt/issues/57.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/uralbash/sqlalchemy_mptt/issues/57#event-1713210852, or mute the thread https://github.com/notifications/unsubscribe-auth/ABxJDIFU45FuKIlswGUwtY8gfuxrq0PIks5uCxo4gaJpZM4UEiIp .

-- Sylvain