简体   繁体   English

与SQLAlchemy建立多对多关系

[英]Adding to a Many to Many relationship with SQLAlchemy

Premise: Search public comments (a string) for instances of items from a predetermined list. 前提:在公共评论(字符串)中搜索预定列表中项目的实例。 There can be multiple list matches within a single comment. 单个评论中可以有多个列表匹配项。

I am trying to use a Many to Many structure to keep track of this. 我正在尝试使用“ 多对多”结构来跟踪此情况。

I have created the following database structure using SQLAlchemy (Python 3.5) 我已经使用SQLAlchemy(Python 3.5)创建了以下数据库结构

reddit_assoc = Table('reddit_assoc', Base.metadata,
    Column('comment_id', Integer, ForeignKey('reddit_comments.comment_id')),
    Column('character_id', Integer, ForeignKey('characters.character_id'))
    )

class characters(Base):
    __tablename__ ='characters'

    character_id = Column(VARCHAR(20),primary_key=True)
    name = Column(VARCHAR(3072))
    added = Column('added', DateTime, default=datetime.datetime.now())
    reddit_mentions = relationship('reddit_comments', secondary='reddit_assoc', back_populates='character_mentions')

class reddit_comments(Base):
    __tablename__ = 'reddit_comments'
    comment_id = Column(VARCHAR(50), primary_key=True)
    comment_author = Column(VARCHAR(300))
    comment_created = Column(VARCHAR(300))
    link_id = Column(VARCHAR(50))
    subreddit_id = Column(VARCHAR(50))
    character_mentions = relationship('characters', secondary='reddit_assoc', back_populates='reddit_comments')

And using the following to find matches 并使用以下内容查找匹配项

def char_counter(comment):
    Session = DBSession()
    reader = Session.query(characters).all()

    for char in reader:
        if char[0] in comment['comment_body'] or char[1] in comment['comment_body']:
            # We have a match. Add to database.
            Session.merge(reddit_comments(#relevant information from comment#))
            #How do I add to the Many to Many here?
            Session.commit()
        Session.close()

Problem: Looking at the comment in the above snippet, I don't understand how I add the relationship of potentially multiple character matches from the comment['comment_body'] string into the reddit_assoc assocation table correctly. 问题:查看以上代码段中的注释,我不明白如何将注释['comment_body']字符串中可能存在多个字符匹配的关系正确添加到reddit_assoc关联表中。 Can someone please advise further? 有人可以进一步建议吗?

Relationships that you are using in this case behave as a list. 在这种情况下,您使用的关系表现为列表。 So you need to add newly created reddit comment to list reddit_mentions . 因此,您需要添加新创建的reddit注释以列出reddit_mentions

def char_counter(comment):
    Session = DBSession()
    reader = Session.query(characters).all()

    for char in reader:
        if char[0] in comment['comment_body'] or char[1] in comment['comment_body']:
            # We have a match. Add to database.
            rc = reddit_comments(#relevant information from comment#)
            Session.flush()  # to ensure you have primary key, although may not be needed
            char.reddit_mentions.append(rc)  # this will eventually fill your reddit_assoc table
            Session.add(char)

    # move this outside of loop        
    Session.commit()
    Session.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM