3
我想覆蓋SQLAlchemy列以在測試數據庫中擁有不同的屬性。基本上,我在我所有的MySQL表中都有一個複合主鍵:一個自動增量BIGINT id
和一個DATETIME create_time
。對於單元測試,我想使用SQLite,但是我的研究讓我相信在這個用例中autoincrement是不可能的。由於除了在MySQL中創建滾動分區以外,我不使用我的create_time
列,所以我想重寫單元測試中的兩列,以便它們與SQLite兼容。注意:我正在使用Base = declarative_base()
路線,並且我所有的型號都繼承了Base
和PrimaryKeyMixin
。覆蓋用於測試的SQLAlchemy列
在我的models.py中,我創建了所有表繼承的mixin PrimaryKeyMixin
。所有表格還爲__table_args__
組件繼承TABLE_ARGS
。
TABLE_ARGS = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
class PrimaryKeyMixin:
id = Column(BIGINT(unsigned=True), primary_key=True, autoincrement=True)
create_time = Column(DATETIME, primary_key=True, default=func.now())
在我的單元測試,我做了以下內容:
from myapp.models import TABLE_ARGS, PrimaryKeyMixin, Column, INT, DATETIME, func
TABLE_ARGS['sqlite_autoincrement'] = True
PrimaryKeyMixin.id = Column(INT(unsigned=True), primary_key=True, autoincrement=True)
PrimaryKeyMixin.create_date = Column(DATETIME, default=func.now())
TEST_DB = '/tmp/myapp_test.db'
from myapp import db
from myapp.models import Base
db.ENGINE = create_engine(
'sqlite:///%s' % TEST_DB,
encoding='utf-8',
)
Base.metadata.drop_all(db.ENGINE)
Base.metadata.create_all(db.ENGINE)
然而,當我添加爲每個表回聲=真到發動機,但它仍然是創造一個PRIMARY_KEY(ID,CREATE_TIME) 。