我要創(chuàng)建的表名為“exception”王凑,有三個字段id获雕,created,description岛马,id是主鍵棉姐,現(xiàn)在開始創(chuàng)建表:
首先在model文件夾下面創(chuàng)建exception.py 文件,內(nèi)容如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Alex.zhao added 2017.12.27
from pybossa.core import db
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.dialects.postgresql import TIMESTAMP
from sqlalchemy import Integer, Unicode, UnicodeText, Text, Boolean
from pybossa.model import DomainObject, make_timestamp
class exception(db.Model, DomainObject):
? ? __tablename__ = 'exception'
? ? #: Counter.ID
? ? id = Column(Integer, primary_key=True)
? ? #: UTC timestamp when the counter was created.
? ? created = Column(TIMESTAMP, default=make_timestamp)
? ? #: description 存儲異常相關的信息
? ? description = Column(Text, nullable=False)
然后在pycharm 的Terminal里面執(zhí)行命令
alembic revision -m"create exception table"
這個時候你會看到如下輸出:
(env)?? stardust_server git:(master) ? alembic revision -m"create exception table"
? Generating /Users/careerman/Desktop/python/stardust/stardust_server/alembic/versions/d7e84e8ca4e8_create_exception_table.py ... done
恩,就像你看到的啦逆,在上面的路徑下面生成了新的文件d7e84e8ca4e8_create_exception_table.py伞矩, 內(nèi)容如下:
# revision identifiers, used by Alembic.
revision = '708261bf5aad'
down_revision = 'd5b6cfdade09'
def upgrade():
? ? pass
def downgrade():
? ? pass
這個時候需要我們手動創(chuàng)建表中的字段,我結合自己的需求參考其他文件夏志,經(jīng)過修改之后變成如下:
# revision identifiers, used by Alembic.
revision = '708261bf5aad'
down_revision = 'd5b6cfdade09'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy import Sequence
from sqlalchemy.sql.ddl import CreateSequence
def upgrade():
? ? op.create_table('exception',
? ? ? ? ? ? ? ? ? ? sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
? ? ? ? ? ? ? ? ? ? sa.Column('created', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
? ? ? ? ? ? ? ? ? ? sa.Column('description', sa.TEXT(), autoincrement=False, nullable=False),
? ? ? ? ? ? ? ? ? ? )
def downgrade():
? ? op.drop_table('exception')
恩乃坤,基本上來說目前這一的表機構滿足我的需求,接下來我在pycharm 的Terminal里面執(zhí)行命令
(env)?? stardust_server git:(master) ? alembic upgrade head
INFO? [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO? [alembic.runtime.migration] Will assume transactional DDL.
INFO? [alembic.runtime.migration] Running upgrade d5b6cfdade09 -> 708261bf5aad, create exception table
到此為止沟蔑,我們的生成表操作就完成了湿诊,可以在右側數(shù)據(jù)表中刷新數(shù)據(jù)庫,就可以看到我們要生成的表exception
我將在異常裝飾器中使用插入表操作瘦材,exception_decorator.py 是異常裝飾器文件
先引入異常模型類
from pybossa.model.exception import exception
再引入我們的全局db句柄
from pybossa.extensions import db
然后我們生成模型
try:
? excpt = exception(description=trace_string.strip())
? ?db.session.add(excpt)
? db.session.commit()
except Exception:
? db.session.rollback()
? raise Exception('exception insert db error')
以上代碼執(zhí)行好枫吧,基于上面的步驟會異常,原因是異常模型里面的id是空值宇色,解決方案就是重新修改upgrade()函數(shù)中的代碼
op.execute(CreateSequence(Sequence("exception_id_seq")))
op.create_table('exception',
sa.Column('id', sa.INTEGER(), server_default=sa.text(u"nextval('exception_id_seq'::regclass)"),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nullable=False),
sa.Column('created', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
sa.Column('description', sa.TEXT(), autoincrement=False, nullable=False),
)
必須加入Sequence模塊九杂,這樣才能自動填充id的值
上面就是我創(chuàng)建一個表的整個過程颁湖,希望給大家一個參考。