Python sqlalchemy 模块,DECIMAL 实例源码
我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用sqlalchemy.DECIMAL。
def upgrade():
op.create_table(
"orders",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(),
sa.Column("updated_at",
sa.PrimaryKeyConstraint("id")
)
op.create_table(
"order_details",
sa.Column("order_id",
sa.Column("product_id", sa.String(),
sa.Column("price", sa.DECIMAL(18, 2),
sa.Column("quantity",
sa.PrimaryKeyConstraint("id"),
sa.ForeignKeyConstraint(
["order_id"], ["orders.id"],
name="fk_order_details_orders"
),
)
def daily_market_table(Metadata, market):
table_name = history_key(market)
# Index columns
date_col = sa.Column('Date', sa.DATETIME, nullable=False)
asset_col = sa.Column(
config.ASSET_ID,
sa.INTEGER,
sa.ForeignKey(config.ASSETS + '.' + config.ID),
nullable=False
)
return sa.Table(
table_name,
Metadata,
date_col,
asset_col,
sa.Column('Units', sa.INTEGER,
sa.Column('$Volume',
sa.Column('LowPrice',
sa.Column('HighPrice',
sa.Column('AvgPrice', sa.DECIMAL,
sa.Column('LastPrice', nullable=True), # Null == NaN?
sa.Index('idx', asset_col, date_col, unique=True),
)
def quotes_table(Metadata, market):
table_name = quote_key(market)
# Index columns
ts_col = sa.Column('Timestamp', sa.TIMESTAMP,
ts_col,
sa.Column('Bid', # No example of nullable
sa.Column('Ask',
sa.Column('Last',
sa.Column('Low',
sa.Column('High',
sa.Column('Average',
sa.Index('idx', ts_col,
)
def upgrade():
"""Upgrade instructions."""
op.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')
op.create_table(
'Feed',
sa.Column('id', postgresql.UUID(), server_default=sa.text('uuid_generate_v4()'),
sa.Column('name', sa.Unicode(),
sa.Column('url',
sa.PrimaryKeyConstraint('id', name=op.f('pk_Feed')),
sa.UniqueConstraint('url', name=op.f('uq_Feed_url'))
)
op.create_table(
'user',
sa.Column('email',
sa.Column('password', name=op.f('pk_user')),
sa.UniqueConstraint('email', name=op.f('uq_user_email'))
)
op.create_table(
'article',
sa.Column('Feed_id',
sa.Column('title',
sa.Column('html_text',
sa.Column('clean_text',
sa.Column('publication_date', postgresql.TIMESTAMP(timezone='UTC'),
sa.ForeignKeyConstraint(['Feed_id'], ['Feed.id'], name=op.f('fk_article_Feed_id_Feed')), name=op.f('pk_article'))
)
op.create_table(
'subscription',
sa.Column('user_id', name=op.f('fk_subscription_Feed_id_Feed')),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], name=op.f('fk_subscription_user_id_user')), name=op.f('pk_subscription'))
)
op.create_table(
'rating',
sa.Column('article_id',
sa.Column('user_rating', sa.DECIMAL(precision=4, scale=2),
sa.Column('machine_rating',
sa.Column('read', sa.Boolean(), server_default=sa.text('FALSE'),
sa.ForeignKeyConstraint(['article_id'], ['article.id'], name=op.f('fk_rating_article_id_article')), name=op.f('fk_rating_Feed_id_Feed')), name=op.f('fk_rating_user_id_user')), name=op.f('pk_rating'))
)