Alembic 自动生成不检测父 class 上的列检测(Alembic + Ormar ORM)
Alembic autogenerate doesn't detect column detect on parent class (Alembic + Ormar ORM)
这是我想要实现的,我想要基本模型内的模型公共字段,即 BaseModel
,如下图所示。
混合
class TimeStampMixin:
created: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
updated: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
class IdMixin:
id: uuid.UUID = ormar.UUID(default=uuid.uuid4, primary_key=True)
基础模型
class BaseModel(ormar.Model, IdMixin, TimeStampMixin):
...
混凝土类
class Concrete(BaseModel):
class Meta(BaseMeta)
我希望将所有 id
、created
、updated
添加到自动生成的迁移中,但下面是 alembic revision --autogenerate
[= 的输出21=]
Alembic 输出
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('concretes',
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('concretes')
注意:当直接添加到Concrete
模型时,所有变化都被完美检测到,但我有很多类不想重复自己。
有人可以帮忙 link 或解释一下如何实现吗?
我end-up 使用抽象模型而不是 Mixins (https://collerek.github.io/ormar/models/inheritance/)。希望对其他人有所帮助。
class TimeStampMixin(ormar.Model):
created: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
updated: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
class Meta(ormar.ModelMeta):
abstract = True
...
class IdMixin(ormar.Model):
id: uuid.UUID = ormar.UUID(default=uuid.uuid4, primary_key=True)
class Meta(ormar.ModelMeta):
abstract = True
...
这是我想要实现的,我想要基本模型内的模型公共字段,即 BaseModel
,如下图所示。
混合
class TimeStampMixin:
created: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
updated: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
class IdMixin:
id: uuid.UUID = ormar.UUID(default=uuid.uuid4, primary_key=True)
基础模型
class BaseModel(ormar.Model, IdMixin, TimeStampMixin):
...
混凝土类
class Concrete(BaseModel):
class Meta(BaseMeta)
我希望将所有 id
、created
、updated
添加到自动生成的迁移中,但下面是 alembic revision --autogenerate
[= 的输出21=]
Alembic 输出
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('concretes',
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('concretes')
注意:当直接添加到Concrete
模型时,所有变化都被完美检测到,但我有很多类不想重复自己。
有人可以帮忙 link 或解释一下如何实现吗?
我end-up 使用抽象模型而不是 Mixins (https://collerek.github.io/ormar/models/inheritance/)。希望对其他人有所帮助。
class TimeStampMixin(ormar.Model):
created: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
updated: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
class Meta(ormar.ModelMeta):
abstract = True
...
class IdMixin(ormar.Model):
id: uuid.UUID = ormar.UUID(default=uuid.uuid4, primary_key=True)
class Meta(ormar.ModelMeta):
abstract = True
...