这是我数据库的当前结构(我正在使用 Flask-SQLAlchemy ):
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, nullable=False)
photos = db.relationship('ProductPhoto', backref='product', lazy=True)
product_size_colors = db.relationship('ProductSizeColor', backref='product', lazy=True)
class Size(db.Model):
id = db.Column(db.Integer, primary_key=True)
size = db.Column(db.String(5), nullable=False)
class Color(db.Model):
id = db.Column(db.Integer, primary_key=True)
color = db.Column(db.String(25), nullable=False)
class ProductSizeColor(db.Model):
__tablename__ = "product_size_color"
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)
size_id = db.Column(db.Integer, db.ForeignKey('size.id'), nullable=False)
color_id = db.Column(db.Integer, db.ForeignKey('color.id'), nullable=False)
quantity = db.Column(db.Integer, nullable=False)
class ProductPhoto(db.Model):
__tablename__ = "product_photo"
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)
photo = db.Column(db.Text, nullable=False)
当前数据库将产品照片(ProductPhoto
)链接到特定产品(Product
),如果产品是带有三种颜色的T恤,如何将数据库调整为识别照片的颜色?
谢谢
答案 0 :(得分:0)
将此属性添加到您的ProductSizeColor类:
color_rel = db.relationship('Color',backref ='product_sc')
然后您可以使用以下方法访问T恤的颜色:
# instantiate for example some object
product = Product.query.filter_by(name='T-Shirt').first()
# access color of your T-shirt
color_t_shirt = product.product_color_size.color_rel.color