- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有这样的表
class Base(object):
id = Column(Integer, primary_key=True)
date_updated = Column(Date, nullable=True)
rating = Column(Integer, nullable=False, default=0)
status = Column(SmallInteger, nullable=False, default=0)
@declared_attr
def __tablename__(cls):
return "table_%s" % cls.LANG
@declared_attr
def __table_args__(cls):
return (
Index('ix__%s__rating' % cls.__tablename__, 'rating'),
Index(
'ix__%s__status' % cls.__tablename__,
'status',
postgresql_where=column('status') == 0,
),
Index(
'ix__%s__date_updated' % cls.__tablename__,
'date_updated',
postgresql_where=column('date_updated') != None,
),
)
class TableEn(Base, db.Model):
LANG = 'en'
class TableUk(Base, db.Model):
LANG = 'uk'
我找到了如何在父 __table_args__
中创建诸如 status
和 date_updated
之类的(部分)索引。
但我需要创建desc排序索引rating
,比如func.desc(rating)
,但我不知道该怎么做那。
没有一个变体对我有用(变体和它的错误):
Index('ix__%s__rating' % cls.__tablename__, 'rating desc')
KeyError: 'rating desc'
Index('ix__%s__rating' % cls.__tablename__, cls.rating.desc())
sqlalchemy.exc.ArgumentError: 无法将未命名的列添加到列集合
Index('ix__%s__rating' % cls.__tablename__, desc('rating'))
在数据库中创建模式时
sqlalchemy.exc.ProgrammingError: (ProgrammingError) ")"处或附近的语法错误第 1 行:在 table_en () 上创建索引 ix__table_en__rating
当然,我可以使用直接 SQL 手动创建该索引,但我确信某处存在解决方案。
提前致谢!
最佳答案
这里简化了代码,做你想做的事:
class BaseModel(Base):
__abstract__ = True
id = Column(Integer, primary_key=True)
rating = Column(Integer, nullable=False, default=0)
@declared_attr
def __tablename__(cls):
return "table_%s" % cls.LANG
@classmethod
def build_indexes(cls):
Index('ix__%s__rating' % cls.__tablename__, cls.__table__.c.rating.desc())
@event.listens_for(BaseModel, 'instrument_class', propagate=True)
def receive_mapper_configured(mapper, class_):
class_.build_indexes()
class TableEn(BaseModel):
LANG = 'en'
class TableUk(BaseModel):
LANG = 'uk'
完整代码 here .
这里有 2 个问题。首先,您应该针对表列而不是映射器对象属性调用 desc
。但是如果你这样做,你就会遇到问题,因为 __table__
属性是在从 __table__args__
获取信息后创建的。所以你需要在创建 __table__
字段后创建索引。一种方法是通过 sqlalchemy 事件。
关于python - __table_args__ 的 Sqlalchemy 声明式继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27211361/
我发现如果 __table_args__ 存在于单表继承设置中的子类中,SQLAlchemy 会抛出 sqlalchemy.exc.ArgumentError: Can't place __table
我没有使用 sqlalchemy 的经验,我有以下代码: class ForecastBedSetting(Base): __tablename__ = 'forecast_bed_set
假设我有这样的表 class Base(object): id = Column(Integer, primary_key=True) date_updated = Column(Da
是否可以使用外键引用并在 sqlalchemy 中的同一个表上有 oracle 分区? 这是在 __table_args__ 中将 oracle 分区定义为字典的方式 class SQLAlchemy
我正在尝试在基类中使用单个 CheckConstraint 创建基模型。当我将基类混合到 flask-sqlalchemy 模型中并尝试使用它时,我得到“无法将未命名的列添加到列集合”。 我试着用不同
我正在尝试使用flask、sqlalchemy 和flask_migrate ... 但是每次运行 manage.py migrate 时,alembic 总是将我的模型检测为新表。 我想我把 tab
在 SQLAlchemy 中,我已经阅读了如何在声明声明时组合来自不同 mixin 的 __table_args__。 Combining Table/Mapper Arguments from Mu
我有以下 SQLAlchemy 模型。已成功迁移至数据库: class MyClassA(db.Model, Timestamp): a_id = db.Column(db.Integer,
我是一名优秀的程序员,十分优秀!