你好,我正在为博客文章创建一个 API,我想像那样创建表格
Post(id, title, subtitle, description, content, main_img, thumbnail_img, author, date) 这是它在 sqlalchemy 中的模型:
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
subtitle = db.Column(db.String(100))
description = db.Column(db.Text, nullable=True)
content = db.Column(db.Text)
main_image_id = db.Column(db.Integer, db.ForeignKey('image_details.id'))
main_image = db.relationship('ImageDetails', back_populates='posts')
thumbnail_image_id = db.Column(db.Integer, db.ForeignKey('image_details.id'))
thumbnail_image = db.relationship('ImageDetails', back_populates='posts')
author_id = db.Column(db.Integer, db.ForeignKey('authors.id'))
author = db.relationship('Author', back_populates='posts')
date = db.Column(db.DateTime, default=datetime.now())
@classmethod
def get_all(cls):
return cls.query.all()
@classmethod
def get_by_id(cls, id):
return cls.query.get(id)
@classmethod
def get_by_author_id(cls, author_id):
return cls.query.filter_by(author_id=author_id).all()
@classmethod
def get_by_title(cls, title):
return cls.query.filter_by(title=title).all()
def add_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
我想将它与看起来像这样的 ImageDetails 表联系起来
ImageDetails(id, url_to_img_file, name, alt,) 这是模型的样子:
class ImageDetails(db.Model):
__tablename__ = 'image_details'
id = db.Column(db.Integer, primary_key=True)
url = db.Column(db.String(255), nullable=False)
name = db.Column(db.String(255), nullable=False)
alt = db.Column(db.String(255), nullable=False)
@classmethod
def get_all(cls):
return cls.query.all()
@classmethod
def get_by_id(cls, id):
return cls.query.get_or_404(id)
def add_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
但是我收到这个错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Post.main_image - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
我已经尝试过那样使用外键,但没有用
main_image = db.relationship('ImageDetails', back_populates='posts', foreign_keys=[main_image_id])
thumbnail_image = db.relationship('ImageDetails', back_populates='posts', foreign_keys=[thumbnail_image_id])
但是我得到了这个错误:
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper[ImageDetails(image_details)]' has no property 'posts'. If this property was indicated from other mappers or configure events, ensure registry.configure() has been called.
也喜欢这个
main_image = db.relationship('ImageDetails', back_populates='posts', foreign_keys="[Post.main_image_id]")
thumbnail_image = db.relationship('ImageDetails', back_populates='posts', foreign_keys="[Post.thumbnail_image_id]")
我得到了这个错误:
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper[ImageDetails(image_details)]' has no property 'posts'. If this property was indicated from other mappers or configure events, ensure registry.configure() has been called.
我试图让用户添加图片,然后当他添加帖子时,他可以从添加的图片中选择一张作为缩略图,一张作为主图。他添加图像时不需要设置关系,但在添加帖子时设置它。