如何在项目中创建一张表
我要创建的表名为“exception”,有三个字段id,created,description,id是主键,现在开始创建表:
首先在model文件夹下面创建exception.py 文件,内容如下:
#!/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里面执行命令
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, 内容如下:
# revision identifiers, used by Alembic.
revision = '708261bf5aad'
down_revision = 'd5b6cfdade09'
def upgrade():
pass
def downgrade():
pass
这个时候需要我们手动创建表中的字段,我结合自己的需求参考其他文件,经过修改之后变成如下:
# 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里面执行命令
(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
到此为止,我们的生成表操作就完成了,可以在右侧数据表中刷新数据库,就可以看到我们要生成的表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')
以上代码执行好,基于上面的步骤会异常,原因是异常模型里面的id是空值,解决方案就是重新修改upgrade()函数中的代码
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的值
上面就是我创建一个表的整个过程,希望给大家一个参考。