Alembic – 如何创建超级表

huangapple go评论78阅读模式
英文:

Alembic - how to create hypertable

问题

如何使用Alembic生成超级表?我想必须添加一些自定义调用,但应该添加在哪里?我尝试使用event.listen,但Alembic不会注册它。

英文:

How to generate hypertable using Alembic? Some custom call must be added I suppose, but where? I tried event.listen but Alembic does not register it.

答案1

得分: 1

你可以通过在Alembic中添加手动、自定义的迁移操作来创建超级表。

不能自动生成它,因为Alembic没有特定的支持,也不理解超级表。

英文:

You can create hypertables in Alembic by adding manual, custom, migration actions.

You cannot generate it automatically, because there is no specific support in Alembic for TimescaleDB and it does not understand hypertables.

答案2

得分: 0

以下是代码的翻译部分:

def upgrade() -> None:
    op.create_table(
        TABLE_NAME,
        sa.Column('ts', sa.DateTime),
        sa.Column('col1', sa.String(50)),
        sa.Column('col2', sa.String(50)),
        sa.Column('col3', sa.String(50)),
        sa.Column('col4', sa.String(50)),
        sa.Column('col5', sa.Integer),
        sa.Column('col6', sa.Integer),
        schema=SCHEMA,
        if_not_exists=True,
    )

    op.execute(f"SELECT create_hypertable('{SCHEMA}.{TABLE_NAME}', 'ts');")

这是代码的翻译部分,不包括其他内容。

英文:
def upgrade() -> None:
    op.create_table(
        TABLE_NAME,
        sa.Column('ts', sa.DateTime),
        sa.Column('col1', sa.String(50)),
        sa.Column('col2', sa.String(50)),
        sa.Column('col3',sa.String(50)),
        sa.Column('col4',sa.String(50)),
        sa.Column('col5',sa.Integer),
        sa.Column('col6',sa.Integer),
        schema=SCHEMA,
        if_not_exists=True,)

    op.execute(f"SELECT create_hypertable('{SCHEMA}.{TABLE_NAME}', 'ts');")

This worked for me. Make sure you create the hypertable extension if needed or you'll get errors.:

CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

huangapple
  • 本文由 发表于 2023年3月3日 20:40:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定