uralbash / sqlalchemy_mptt

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

A problem when I insert the root #82

Open hanjie-chen opened 1 year ago

hanjie-chen commented 1 year ago

when I try to insert the root node in flask shell, there is something wrong happened

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py", line 553, in commit
    return self._proxied.commit()
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1903, in commit
    trans.commit(_to_root=True)
  File "<string>", line 2, in commit
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/state_changes.py", line 137, in _go
    ret_value = fn(self, *arg, **kw)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1218, in commit
    self._prepare_impl()
  File "<string>", line 2, in _prepare_impl
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/state_changes.py", line 137, in _go
    ret_value = fn(self, *arg, **kw)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1193, in _prepare_impl
    self.session.flush()
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 4155, in flush
    self._flush(objects)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 4292, in _flush
    transaction.rollback(_capture_exception=True)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 4252, in _flush
    flush_context.execute()
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 467, in execute
    rec.execute(self)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 644, in execute
    util.preloaded.orm_persistence.save_obj(
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 54, in save_obj
    save_obj(base_mapper, [state], uowtransaction, single=True)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 60, in save_obj
    for (
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 233, in _organize_states_for_save
    mapper.dispatch.before_insert(mapper, connection, state)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/event/attr.py", line 487, in __call__
    fn(*args, **kw)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/orm/events.py", line 889, in wrap
    fn(*arg, **kw)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy_mptt/events.py", line 574, in before_insert
    mptt_before_insert(mapper, connection, instance)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy_mptt/events.py", line 96, in mptt_before_insert
    select(
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/_selectable_constructors.py", line 493, in select
    return Select(*entities)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/selectable.py", line 5161, in __init__
    self._raw_columns = [
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/selectable.py", line 5162, in <listcomp>
    coercions.expect(
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py", line 413, in expect
    resolved = impl._literal_coercion(
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py", line 652, in _literal_coercion
    self._raise_for_expected(element, argname)
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py", line 1143, in _raise_for_expected
    return super()._raise_for_expected(
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py", line 711, in _raise_for_expected
    super()._raise_for_expected(
  File "/Users/pingguo/opt/anaconda3/envs/Flask_Web/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py", line 536, in _raise_for_expected
    raise exc.ArgumentError(msg, code=code) from err
sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other columns clause element expected, got [<sqlalchemy.sql.elements.BinaryExpression object at 0x7fee25170d90>]. Did you mean to say select(<sqlalchemy.sql.elements.BinaryExpression object at 0x7fee25170d90>)?

and my python code is here

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy_mptt.mixins import BaseNestedSets

app = Flask(__name__)
url = 'sqlite:////' + os.path.join(app.root_path, 'data.db')
app.config['SQLALCHEMY_DATABASE_URI'] = url
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 

db = SQLAlchemy(app)

class Category(db.Model, BaseNestedSets):
    __tablename__ = 'category'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)

    def __repr__(self):
        return '<Category {}>'.format(self.name)

and my python package is here

截屏2023-05-19 20 44 22

I can't figure out where is problem is, did that because the version of Flask-SQLAlchemy?

EchoShoot commented 1 year ago

i have the same problem, it's cause by sqlalchemy version is too high, you can follow this way pip install sqlalchemy~=1.4.48

corentinclichy commented 1 year ago

hey, I have the same issue. Do you know if the issue will be fixed ? Or is there another maintained library that support latest version of SQL Alchemy. Thanks a lot.

EchoShoot commented 1 year ago

the code last update time is 2019, It's been a long time since no one has been maintaining it, there is no way to solve this problem

corentinclichy commented 1 year ago

Yes, but I saw there is a pr for that still waiting for validation

corentinclichy commented 1 year ago

Do you know if there is other maintained lib providing tree structure management ?

tszyan commented 1 year ago

@corentinclichy , if you want to use pr just install sqlalchemy_mptt from pr's branch: https://github.com/lowatt/sqlalchemy_mptt/tree/sqlalchemy

In requirements.txt it will look like: sqlalchemy_mptt @ git+https://github.com/lowatt/sqlalchemy_mptt@sqlalchemy

corentinclichy commented 1 year ago

@tszyan Are you sure that its ok to do that ? seems pretty risky to take deps from a pr IMO

Do you know other lib that provides same feature for sqlalchemy ?