无法正确保持多对多关系

Cannot persist many-to-many relationship correctly

ArticleImage是多对多的关系,中间的table是ArticleImage。我预计我会在 Article table 中看到 1 行,在 Image table 中看到 3 行,在 ArticleImage table 中看到 3 行。 ArticleArticleImage 部分按预期工作,但是,Image table 中有 0 行,[=15= 中有外键列 'image_id' ] table 这 3 个新添加的行是 'NULL'。我缺少什么才能正确地保留 Image table 中的行? (没有打印错误)

我的代码:

db_session = some_function()  # get connection to database here
try:
    article = Article()
    db_session.add(article)
    article.name = 'new post #1'
    article.author = 'pojan'
    article.images.append(Image('img1.jpg'))
    article.images.append(Image('img2.jpg'))
    article.images.append(Image('img3.jpg'))
    db_session.merge(article)
    db_session.commit()

    print db_session.query(Article).count()       # 1
    print db_session.query(ArticleImage).count()  # 3
    print db_session.query(Image).count()         # 0 <- error here, images are not added 

except Exception as e:
    print e

我也尝试了下面的代码,现在 Image 将被添加到数据库中,但是 ArticleImage 中的 image_id 列仍然为 NULL:

    article = Article()
    db_session.add(article)
    article.name = 'new post #1'
    article.author = 'pojan'

    image1 = Image('img1.jpg')
    image2 = Image('img2.jpg')
    image3 = Image('img3.jpg')
    db_session.add(image1)
    db_session.add(image2)
    db_session.add(image3)

    article.images.append(image1)
    article.images.append(image2)
    article.images.append(image3)
    db_session.merge(article)
    db_session.commit()

我的模型:

class Image(EntityClass):
    attributes = ['id', 'name', 'path']

    id = Column(Integer, primary_key=True)
    name = Column(String(255), nullable=False)
    path = Column(String(255), nullable=False, unique=True)

    def __init__(self, name):
        self.name = name
        self.path = '/root/' + name

class ArticleImage(EntityClass):

    article_id = Column(Integer, ForeignKey('article.id'))
    image_id = Column(Integer, ForeignKey('image.id'))
    image = relationship(Image)
    position = Column(Integer)

class Article(EntityClass):

    attributes = ['id','name','author']

    id = Column(Integer, primary_key=True)

    _images = relationship(ArticleImage,
             order_by=[ArticleImage.position],
             collection_class=ordering_list('position'))
    images = association_proxy('_images', 'image')

    name = Column(String(255), nullable=False)
    author = Column(String(255), nullable=False)

(设计是基于我在网上找到的article,并做了一些小改动)

与您给出的示例相比,您在 ArticleImage 中缺少主键。

我会得到

sqlalchemy.exc.ArgumentError: Mapper Mapper|ArticleImage|article_image could not assemble any primary key columns for mapped table 'article_image'

使用您的 ArticleImage 代码。

根据您所指的article,您还应该在ArticleImage中添加一个构造函数,以便在ArticleImage对象时将ArticleImageImage相关联已创建,否则 ArticleImage 将永远不知道它应该引用哪个图像行。

class ArticleImage(EntityClass):

    article_id = Column(Integer, ForeignKey('article.id'))
    image_id = Column(Integer, ForeignKey('image.id'))
    image = relationship(Image)
    position = Column(Integer)

    def __init__(self, image=None):
        self.image = image