【问题标题】:Sqlalchemy: Get some columns aliasedSqlalchemy:获取一些列别名
【发布时间】:2023-03-24 13:28:01
【问题描述】:

我有一个问题,我确信它会很快回答,但我无法通过谷歌搜索找到它......

我有一个使用声明性 SqlAlchemy 定义的类 Product:

class Product(declarativeBase):
    __tablename__ = "products"
    _brand = Column("brand", Unicode(128))
    _series = Column("series", String(128))
    _price = Column("price", Float)
    _description = Column("description", UnicodeText)

    _manufacturerId = Column("manufacturer_id", Integer, ForeignKey("manufacturers.id"), key="manufacturerId")
    _manufacturer = relationship("Manufacturer",
        uselist=False,
         backref=backref("_products", collection_class=set)
         )

在某一点上,我想获取所有产品的一些列,例如 _brand 和 _manufacturerId,但我不想将其命名为 _manufacturerId,而是将其命名为 _manufacturer(主要是为了“隐藏”关系)。

我不知道该怎么做......它会是这样的:

session.query(Product.Product._brand, Product.Product._manufacturerId as "_manufacturer").all()

我很确定这是可行的,但我不知道如何(使用 sqlalchemy 0.6.6,以防万一)

非常感谢!

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    其实...我认为“标签”可能会起作用。

    How do I put a SQLAlchemy label on the result of an arithmetic expression?

    如果有,我会在这里详细说明

    【讨论】:

    • 是的,它有效... session.query(MyClass.name_sample.label('name'))
    猜你喜欢
    • 2012-02-15
    • 2019-02-14
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多