从 sqlalchemy 更改为 postgresql 并得到关系错误

changinge from sqlalchemy to postgresql and got the error with relationship

您好,我有数据库多对多关系和最好的一对一关系,它与 sqlalchemy 完美配合,现在我将其更改为 postgresql 并得到

errornvalidRequestError: 在关系 Users.is_bestfriend 上,'dynamic' 加载程序不能与 many-to-one/one-to-one 关系一起使用 and/or uselist=False。

error:

Traceback (most recent call last)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__

    return self.wsgi_app(environ, start_response)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app

    response = self.make_response(self.handle_exception(e))

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception

    reraise(exc_type, exc_value, tb)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app

    response = self.full_dispatch_request()

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request

    rv = self.handle_user_exception(e)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception

    reraise(exc_type, exc_value, tb)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request

    rv = self.dispatch_request()

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

    File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 756, in decorated_view

    elif not current_user.is_authenticated():

    File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 338, in __getattr__

    return getattr(self._get_current_object(), name)

    File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 297, in _get_current_object

    return self.__local()

    File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 46, in <lambda>

    current_user = LocalProxy(lambda: _get_user())

    File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 794, in _get_user

    current_app.login_manager._load_user()

    File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 363, in _load_user

    return self.reload_user()

    File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 325, in reload_user

    user = self.user_callback(user_id)

    File "/home/peg/flask-Alembic/app/auth/view.py", line 60, in load_user

    return Users.query.get(int(user_id))

    File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy/__init__.py", line 426, in __get__

    mapper = orm.class_mapper(type)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/base.py", line 378, in class_mapper

    mapper = _inspect_mapped_class(class_, configure=configure)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/base.py", line 355, in _inspect_mapped_class

    mapper._configure_all()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1129, in _configure_all

    configure_mappers()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2544, in configure_mappers

    mapper._post_configure_properties()

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1660, in _post_configure_properties

    prop.post_instrument_class(self)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/interfaces.py", line 481, in post_instrument_class

    self.strategy.init_class_attribute(mapper)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/dynamic.py", line 31, in init_class_attribute

    "uselist=False." % self.parent_property)

    InvalidRequestError: On relationship Users.is_bestfriend, 'dynamic' loaders cannot be used with many-to-one/one-to-one relationships and/or uselist=False.

model.py:

friends = db.Table('friends',
    db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
    db.Column('friend_id', db.Integer, db.ForeignKey('users.id'))
    )

    class Users(db.Model):
       id = db.Column(db.Integer, primary_key = True)
       name = db.Column(db.String(50), index=True)
       age= db.Column(db.Integer )
       email = db.Column(db.String(50),index=True, unique= True)
       bestfriend_id = db.Column(db.Integer, db.ForeignKey('users.id'))


       is_bestfriend = db.relationship( 'Users',lazy='dynamic',remote_side=id, post_update=True)

       is_friend = db.relationship('Users', #defining the relationship, Users is left side entity
            secondary = friends, #indecates association table
            primaryjoin = (friends.c.user_id == id), #condition linking the left side entity
            secondaryjoin = (friends.c.friend_id == id),#cond if link right.s ent. with assoc table
            backref = db.backref('friends', lazy = 'dynamic'),#how accessed from right
            lazy = 'dynamic'
        ) 
      #funcitons for bestfriend management
       def are_bestfriends(self, user):
             return self.is_bestfriend == user

        #best friends management
       def be_bestfriend(self, user):
             if not self.are_bestfriends(user):
                      self.is_bestfriend = [user] 
                      user.is_bestfriend = [self]
                      return self

view.py:

#best_freinds
@layout.route('/bestFriend/<name>')
@login_required
def bestFriend(name):
    user = Users.query.filter_by(name = name).first()
    if user is None:
        flash('User %s not found.' % name)
        return redirect(url_for('index'))
    if user == g.user:
        flash('You can\'t Best Friend yourself!')
        return redirect(url_for('user', page=1,sortby='normal'))
    u = g.user.be_bestfriend(user) #got error here if I remove lazy='dynamic'
    if u is None:
        flash('Cannot be best Friend ' + name + '.')
        return redirect(url_for('user', page=1,sortby='normal'))
    db.session.add(u)
    db.session.commit()
    flash('You are now BestFriend with ' + name + '!')
    return redirect(url_for('user', page=1,sortby='normal'))

如错误信息所述,您不能在关系中使用 lazy='dynamic'uselist=False;无论如何它只会加载一个对象,不需要对其进行动态查询。从 is_bestfriend 关系中删除 lazy='dynamic'