Sqlalchemy 具有额外字段的多对多映射

发布于 2024-12-09 16:25:02 字数 1346 浏览 1 评论 0原文

我与 sqlalchemy 创建了一个多对多的关系,如下所示:

subject_books = Table('subject_books', Base.metadata,
    Column('subject_id', Integer, ForeignKey('subjects.id')),
    Column('book_id', Integer, ForeignKey('books.id')),
    Column('group', Integer)
)

class Subject(Base):
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True)
    value = Column(Unicode(255), unique=True)

class Book(Base):
    __tablename__ = 'books'
    id =  Column(Integer, primary_key=True)
    title = Column(Unicode(255))
    isbn = Column(Unicode(24))
    subjects = relationship('Subject', secondary=subject_books, collection_class=attribute_mapped_collection('group'), backref='books')

之后我创建了一个如下所示的测试:

book = Book(title='first book',isbn='test')
book.subjects[0] = Subject(value='first subject')
book.subjects[1] = Subject(value='second subject')

session.add(book)
transaction.commit()

并且它工作正常。但我真正想要的是存储多个具有相同 group 值的主题,所以我尝试了以下测试:

book = Book(title='first book',isbn='test')
book.subjects[0] = [Subject(value='first subject'),Subject(value='second subject')]
book.subjects[1] = [Subject(value='third subject'),Subject(value='forth subject')]

session.add(book)
transaction.commit()

但它不起作用。

这可以使用 sqlalchemy 来完成吗?

提前致谢

I created a many to many relationship with sqlalchemy like this:

subject_books = Table('subject_books', Base.metadata,
    Column('subject_id', Integer, ForeignKey('subjects.id')),
    Column('book_id', Integer, ForeignKey('books.id')),
    Column('group', Integer)
)

class Subject(Base):
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True)
    value = Column(Unicode(255), unique=True)

class Book(Base):
    __tablename__ = 'books'
    id =  Column(Integer, primary_key=True)
    title = Column(Unicode(255))
    isbn = Column(Unicode(24))
    subjects = relationship('Subject', secondary=subject_books, collection_class=attribute_mapped_collection('group'), backref='books')

after that I created a test like following:

book = Book(title='first book',isbn='test')
book.subjects[0] = Subject(value='first subject')
book.subjects[1] = Subject(value='second subject')

session.add(book)
transaction.commit()

and it works fine. But what I really want is to store more than one subject with the same group value, so I tried the following test:

book = Book(title='first book',isbn='test')
book.subjects[0] = [Subject(value='first subject'),Subject(value='second subject')]
book.subjects[1] = [Subject(value='third subject'),Subject(value='forth subject')]

session.add(book)
transaction.commit()

but it does not work.

Can this be done using sqlalchemy?

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-12-16 16:25:02

拉齐

我认为你正在构建错误的关系。

您的关系必须是

book M2M subject

subject M2M group

因此,您必须为群组再创建一个模型,并且必须在中指定为 m2m主题

你的模型会是这样的。

subject_books = Table('subject_books', Base.metadata,
    Column('subject_id', Integer, ForeignKey('subjects.id')),
    Column('book_id', Integer, ForeignKey('books.id')),        
)
subject_group = Table('subject_groups', Base.metadata,
    Column('group_id', Integer, ForeignKey('groups.id')),
    Column('subject_id', Integer, ForeignKey('subjects.id')),
)

class Subject(Base):
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True)
    value = Column(Unicode(255), unique=True)
    groups = relationship('Groups', secondary=subject_groups, backref='subjects')

class Groups(Base):
    __tablename__ = 'groups'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(255), unique=True)

class Book(Base):
    __tablename__ = 'books'
    id =  Column(Integer, primary_key=True)
    title = Column(Unicode(255))
    isbn = Column(Unicode(24))
    subjects = relationship('Subject', secondary=subject_books, backref='books')

我还检查了 attribute_mapped_collection 的文档。但每次我发现每个键只与一个对象相关联,而不是多个。如果您在任何地方阅读过,请提供链接,以便我可以检查它如何适合您的代码。

我想这会对你有所帮助。

Razi

I think you are constructing wrong relation ship.

Your relation ship must be

book M2M subject

subject M2M group

So you have to create one more model for group and that must be assign as m2m in Subject

Your models will be like.

subject_books = Table('subject_books', Base.metadata,
    Column('subject_id', Integer, ForeignKey('subjects.id')),
    Column('book_id', Integer, ForeignKey('books.id')),        
)
subject_group = Table('subject_groups', Base.metadata,
    Column('group_id', Integer, ForeignKey('groups.id')),
    Column('subject_id', Integer, ForeignKey('subjects.id')),
)

class Subject(Base):
    __tablename__ = 'subjects'
    id = Column(Integer, primary_key=True)
    value = Column(Unicode(255), unique=True)
    groups = relationship('Groups', secondary=subject_groups, backref='subjects')

class Groups(Base):
    __tablename__ = 'groups'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(255), unique=True)

class Book(Base):
    __tablename__ = 'books'
    id =  Column(Integer, primary_key=True)
    title = Column(Unicode(255))
    isbn = Column(Unicode(24))
    subjects = relationship('Subject', secondary=subject_books, backref='books')

I also check the docs for attribute_mapped_collection. But each time I found that each key is associated with only one object not more then one. If you read anywhere then please provide the link so I can check that how it will be fit in your code.

I think this will be help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文