【问题标题】:SQLAlchemy inserting data into two tablesSQLAlchemy 将数据插入两个表
【发布时间】:2018-05-17 02:27:52
【问题描述】:

我正在开发我的第一个基于 SQLAlchemy 的应用程序,经过几个小时的文档和一些视频的工作,我仍然无法解决问题。

我的应用是一个简单的 CRUD 购物清单。我想将产品的类别保存在单独的表中,所以这里有 SQLAlchemy 的关系模块。错误消息没有给我任何提示。

engine = create_engine(my_database, echo = True)

connection = engine.connect()

Base = declarative_base()
session = sessionmaker(bind=engine)

class MyEnum(enum.Enum):
    one = "pieces"
    two = "kg"

class ProductTable(Base):
    __tablename__ = 'product'
    product_id = Column(Integer, primary_key=True)
    product_name = Column(String(30), nullable=False)
    product_quantity = Column(Integer, nullable=False)
    product_type = Column(Enum(MyEnum), nullable=False)
    category_id = Column(Integer, ForeignKey('category.id'), nullable=False)
    category = relationship("category", back_populates="product")
    product_description = Column(String(255))


class CategoryTable(Base):
    __tablename__ = 'category'
    id = Column(Integer, primary_key=True)
    category_name = Column(String(25), nullable=False)

Base.metadata.create_all(engine)

session = session()

cc_product = ProductTable(product_id=1,
                          product_name="cucumber",
                          product_quantity="1",
                          product_type="kg",
                          product_description="For the salad")

cc_category= CategoryTable(category_name="vegetables")


session.add(cc_product, cc_category)

session.commit()

我。表的创建顺利完成,没有错误,但是,创建本身是否设计正确?每个产品都有一个类别,但一个类别应分配给一个或多个产品。我是根据一对一的关系制作的。

二。向两个表中插入数据。我想插入如下数据:

  1. Product_id = 1
  2. Product_name = 黄瓜
  3. Product_quantity = 1
  4. Product_type = "kg" 或 "pieces"
  5. 类别 = 蔬菜(来自类别表)
  6. 描述 = “等等等等”

我认为不仅数据插入过程有问题,而且表格创建过程也有问题。

这是错误,tbh 并没有告诉我任何信息:

sqlalchemy.exc.ArgumentError: relationship 'category' expects a class or a mapper argument (received: <class 'sqlalchemy.sql.schema.Table'>)

【问题讨论】:

  • 您有一些小错误,例如将Session.add() 多个实例作为参数传递(实际上不会添加第二个),在关系中使用了错误的目标,这会导致映射器配置跳闸,以及使用@987654327 @ 不定义另一端(而不是 backref)。产品和类别也没有链接。我建议(重新)阅读ORM tutorial
  • 我发现了一些小错误,关于 Session.add() 以及这些表实际上没有链接的事实,但我只是缺乏如何正确设置它们的想法。该文档很棒,但是由于它同时引用了许多不同的概念,所以我很困惑。如果有人可以描述这种特定情况下的这种关系应该是什么样子,我将不胜感激
  • 不要尝试与Table(您在模型中使用__tablename__ 给出的名称)建立关系,而是创建与模型的关系:relationship("CategoryTable", ...)。通过链接,我的意思是你应该在提交之前做cc_product.category = cc_category
  • @IljaEverilä 喜欢这样吗?:category_id = Column(Integer, ForeignKey('product.product_id'))categories = relationship("CategoryTable",backref='product')

标签: python sqlalchemy


【解决方案1】:

你有两个错误:

  1. 您将 "category" 写为 Mapper 类,而不是 "CategoryTable"
  2. 忘记在“CategoryTable”上创建产品
class ProductTable(Base):
    __tablename__ = 'product'
    product_id = Column(Integer, primary_key=True)
    product_name = Column(String(30), nullable=False)
    product_quantity = Column(Integer, nullable=False)
    product_type = Column(Enum(MyEnum), nullable=False)
    category_id = Column(Integer, ForeignKey('category.id'), nullable=False)
    categories = relationship("CategoryTable", back_populates="products")
    product_description = Column(String(255))


class CategoryTable(Base):
    __tablename__ = 'category'
    id = Column(Integer, primary_key=True)
    category_name = Column(String(25), nullable=False)
    products = relationship('ProductTable', back_populates='categories')

还需要进行一些更改:

  • CategoryTable 更改为Category(也适用于ProductTable,更好的名称)
  • 在让事情运行后,约束会失败...

【讨论】:

  • 我已经设法通过这种方式启动并运行它:pastebin.com/k1j0KRAM 尽管我不完全理解为什么但它有效..
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多