无法确定关系Product.collections上的父/子表之间的联接条件

2024-09-30 01:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

产品和收藏之间的关系是多对多的。一个产品可以有多种形式 集合和每个集合可以有许多产品。对于这种情况,我想在fastapi中将django模型转换为sqlalchemy。下表列出了django车型的产品和系列

class Product(models.Model):
    product_type = models.ForeignKey(
        ProductType, related_name="products", on_delete=models.CASCADE
    )
    name = models.CharField(max_length=250)
    slug = models.SlugField(max_length=255, unique=True, allow_unicode=True)


class CollectionProduct(models.Model):
    collection = models.ForeignKey(
        "Collection", related_name="collectionproduct", on_delete=models.CASCADE
    )
    product = models.ForeignKey(
        Product, related_name="collectionproduct", on_delete=models.CASCADE
    )

class Collection(models.Model):
    name = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=255, unique=True, allow_unicode=True)
    products = models.ManyToManyField(
        Product,
        blank=True,
        related_name="collections",
        through=CollectionProduct,
        through_fields=("collection", "product"),
    )
    background_image = VersatileImageField(
        upload_to="collection-backgrounds", blank=True, null=True
    )

我尝试了以下方法将django模型迁移到sqlalchemy。然而,我得到了以下错误

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Product.collections - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

炼金术

class Product(Base, TimestampMixin):
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(255), nullable=False)
    slug = Column(String, index=True, unique=True)
    # referencing the parent
    product_type_id = Column(Integer, ForeignKey("producttype.id"))
    product_type = relationship("ProductType", back_populates="products")

    collections = relationship(
        "Collection",
        back_populates="product",
    )
    collectionproduct = relationship("CollectionProduct", back_populates="product")



class Collection(Base, TimestampMixin):
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(255), nullable=False)
    slug = Column(String, index=True, unique=True)
    product = relationship(
        "Product",
        # secondary=ProductCollection.__tablename__,
        back_populates="collections",
    )
    collectionproduct = relationship("CollectionProduct", back_populates="collection")


class CollectionProduct(Base, TimestampMixin):
    id = Column(Integer, primary_key=True, index=True)
    collection_id = Column(Integer, ForeignKey("collection.id"))
    product_id = Column(Integer, ForeignKey("product.id"))

    collection = relationship("Collection", back_populates="collectionproduct")
    product = relationship("Product", back_populates="collectionproduct")

如何克服连接问题,在sqlalchemy中迁移产品和集合的django模型


Tags: nameidtruemodelsbackcolumnintegerproduct

热门问题