给定一个声明性 SQLAlchemy 类,我如何以编程方式检索作为主键的 Column 对象的列表?

发布于 2025-01-08 02:00:51 字数 379 浏览 0 评论 0原文

假设我在 SQLAlchemy 中有一个类,

class Note(Base):
    __tablename__ = 'notes'
    slug = Column('short_title', String, primary_key=True)
    date = Column('day', Date, primary_key=True)
    contents = Column(Text, nullable=True)

仅给出对 Note 类的引用,函数如何根据它们构成相应表的主键来构建列表 ['Note.slug', 'Note.date'] ?

此外,有没有办法根据列属性过滤类属性? (例如可为空、类型、唯一性、索引中的成员资格等)

Say I have a class in SQLAlchemy like

class Note(Base):
    __tablename__ = 'notes'
    slug = Column('short_title', String, primary_key=True)
    date = Column('day', Date, primary_key=True)
    contents = Column(Text, nullable=True)

Given only a reference to the Note class, how could a function build the list ['Note.slug', 'Note.date'] predicated on their constituting the corresponding table's primary key?

Furthermore, is there a way to filter class attributes based on column properties? (such as nullable, type, uniqueness, membership in an index, etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可可 2025-01-15 02:00:51

试试这个来找到主要字段。

from sqlalchemy import create_engine

engine = create_engine('sqlite:///:memory:', echo=True)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, Text
from sqlalchemy.orm.attributes import InstrumentedAttribute


Base = declarative_base()

class Note(Base):
    __tablename__ = 'notes'
    slug = Column('short_title', String, primary_key=True)
    date = Column('day', Date, primary_key=True)
    contents = Column(Text, nullable=True)

if __name__ == '__main__':
    import inspect
    predicate = lambda x: isinstance(x, InstrumentedAttribute)

    fields = inspect.getmembers(Note, predicate=predicate)

    fields_vars = [vars(y.property.columns[0]) for x,y in fields]

    primary_fields = [x['name'] for x in fields_vars if x['primary_key']]

    print "All Fields :", fields_vars

    print "Primary Fields :", primary_fields

这样您就可以从模型中找到任何类型的字段。

Try this to find the primary fields.

from sqlalchemy import create_engine

engine = create_engine('sqlite:///:memory:', echo=True)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, Text
from sqlalchemy.orm.attributes import InstrumentedAttribute


Base = declarative_base()

class Note(Base):
    __tablename__ = 'notes'
    slug = Column('short_title', String, primary_key=True)
    date = Column('day', Date, primary_key=True)
    contents = Column(Text, nullable=True)

if __name__ == '__main__':
    import inspect
    predicate = lambda x: isinstance(x, InstrumentedAttribute)

    fields = inspect.getmembers(Note, predicate=predicate)

    fields_vars = [vars(y.property.columns[0]) for x,y in fields]

    primary_fields = [x['name'] for x in fields_vars if x['primary_key']]

    print "All Fields :", fields_vars

    print "Primary Fields :", primary_fields

This way you can find any type of fields from your model.

浪漫之都 2025-01-15 02:00:51

这将为您提供一个主键列名称列表:

from sqlalchemy.orm import class_mapper

primary_keys = [key.name for key in class_mapper(Note).primary_key]

this will give you a list of column names which are primary_keys:

from sqlalchemy.orm import class_mapper

primary_keys = [key.name for key in class_mapper(Note).primary_key]
画中仙 2025-01-15 02:00:51

不完全是所需要的,而是一个更短的变体,仅使用内部信息:

from sqlalchemy import create_engine

engine = create_engine('sqlite:///:memory:', echo=True)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, Text

Base = declarative_base()

class Note(Base):
    __tablename__ = 'notes'
    slug = Column('short_title', String, primary_key=True)
    date = Column('day', Date, primary_key=True)
    contents = Column(Text, nullable=True)

if __name__ == '__main__':
    print [(i.name, i.primary_key) for i in Note.__table__.columns]

这给了我们以下结果:

[('short_title', True), ('day', True), ('contents', False)]

Not exactly what's needed, but a bit shorter variant that's only using inside information:

from sqlalchemy import create_engine

engine = create_engine('sqlite:///:memory:', echo=True)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, Text

Base = declarative_base()

class Note(Base):
    __tablename__ = 'notes'
    slug = Column('short_title', String, primary_key=True)
    date = Column('day', Date, primary_key=True)
    contents = Column(Text, nullable=True)

if __name__ == '__main__':
    print [(i.name, i.primary_key) for i in Note.__table__.columns]

This gives us following result:

[('short_title', True), ('day', True), ('contents', False)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文