从sql-alchemy

时间:2015-08-02 02:51:43

标签: python flask sqlalchemy flask-sqlalchemy

我正在尝试删除 flask-sqlalchemy 中的表条目,但是我收到以下错误。

AttributeError: 'Join' object has no attribute 'delete'

(我在下面提供了整个错误消息)。我想删除的表是

class Groups(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    leader_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    name = db.Column(db.String(1000))
    start_datetime = db.Column(db.DateTime)
    email_notification = db.Column(db.String(6))
    member_count = db.Column(db.Integer)
    new_member_confirmed = db.Column(db.SmallInteger)
    next_jc = db.Column(db.DateTime)
    last_jc = db.Column(db.DateTime)
    meeting_location = db.Column(db.String(1000))
    meeting_time = db.Column(db.DateTime)
    meeting_period = db.Column(db.String(10))
    meeting_timezone = db.Column(db.String(1000))
    meeting_weekday = db.Column(db.String(1000))

    vote_list = db.relationship('Papers',
                            secondary="join(Papers, VoteList, Papers.arxiv_id == VoteList.arxiv_id)",
                            primaryjoin="and_(Groups.id == VoteList.group_id)") 
    members = db.relationship('User',
                          secondary="join(Members, User, User.id == Members.user_id)",
                          primaryjoin="and_(Groups.id == Members.group_id)") 

    def __repr__(self):
        return '<Groups %r>' % (self.id)

以下是在此

中发挥作用的其他表格
class VoteList(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
    arxiv_id = db.Column(db.String(1000), db.ForeignKey('papers.arxiv_id'))
    vote_datetime = db.Column(db.DateTime)

    def __repr__(self):
        return '<VoteList %r>' % (self.id)

class Members(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))

    def __repr__(self):
         return '<Members %r>' % (self.id)

class User(db.Model):
    __searchable__ = ['username','email','position','institute','id']

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    start_datetime = db.Column(db.DateTime)
    password = db.Column(db.String(1000))
    position = db.Column(db.String(1000))
    institute = db.Column(db.String(1000))
    interests = db.Column(db.String(10000))
    CV_path = db.Column(db.String(1000))
    profile_picture_path = db.Column(db.String(1000))
    github_account = db.Column(db.String(1000))
    linkedin_account = db.Column(db.String(1000))
    jc_count = db.Column(db.Integer)
    query_count = db.Column(db.Integer)
    last_seen = db.Column(db.DateTime)
    confirmed = db.Column(db.SmallInteger) # use this instead of db.Boolean... otherwise there is an error in db.migrade

    followed = db.relationship('User', 
                           secondary=followers, 
                           primaryjoin=(followers.c.follower_id == id), 
                           secondaryjoin=(followers.c.followed_id == id), 
                           backref=db.backref('followers', lazy='dynamic'), 
                           lazy='dynamic')
    user_queries = db.relationship('Queries', backref='user')
    reading_list = db.relationship('Papers', 
                               secondary="join(Papers, ReadingList,     Papers.arxiv_id == ReadingList.arxiv_id)",
                               primaryjoin="and_(User.id ==     ReadingList.user_id)")
    group_leader = db.relationship('Groups', backref='user')
    vote_list = db.relationship('Papers', 
                            secondary="join(Papers, VoteList, Papers.arxiv_id == VoteList.arxiv_id)",
                            primaryjoin="and_(User.id == VoteList.user_id)")
    group_memberships = db.relationship('Groups',
                                    secondary="join(Members, Groups, Members.group_id == Groups.id)",
                                    primaryjoin="and_(User.id == Members.user_id)") 

我的删除尝试是

>>> groups = models.Groups.query.all()
>>> groups
[<Groups 1>, <Groups 2>]
>>> db.session.delete(groups[1])
>>> db.session.commit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 150, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 790, in commit
    self.transaction.commit()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 392, in commit
    self._prepare_impl()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
    self.session.flush()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2004, in flush
    self._flush(objects)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2122, in _flush
    transaction.rollback(_capture_exception=True)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2086, in _flush
    flush_context.execute()
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 373, in execute
    rec.execute(self)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 485, in execute
    self.dependency_processor.process_deletes(uow, states)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/dependency.py", line 1023, in process_deletes
    secondary_update, secondary_delete)
  File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/dependency.py", line 1098, in _run_crud
    statement = self.secondary.delete(sql.and_(*[
AttributeError: 'Join' object has no attribute 'delete'

任何想法在这里发生了什么? 卡尔

1 个答案:

答案 0 :(得分:0)

通过阅读错误代码,似乎SQL-Alchemy也试图删除Group引用的其他表中的行。但是,因为引用似乎返回一个连接,它没有删除函数,因为连接中的行不是表中的行而是不同表的行的组合,会引发错误。

我无法找到您的错误解决方案,因为我无法复制它,因为我不知道关系中使用的其他表的模型。如果您为PapersVoteListMembersUser添加模型,我将有更好的机会找到解决方案。

作为旁注: 你想要达到什么样的关系?像这样的连接是非常先进的,并且大多数时候有更简单的方法来实现期望的结果。在Group.members的情况下,最好是从MemberUserMemberGroup建立外键关系,后者与backref(docs&amp; api)?然后,您可以使用与此类似的代码来获取所有成员:

# somegroup = Group()
users = [member.user for member in somegroup.member]

修改: 根据您的模型判断,您似乎想要Groups.members的多对多关系(tutorial)。