为什么Django允许您重置postgres和其他DBMS上的sequences(AutoID)字段,而不允许SQLite3?在
查看django/db/backends/sqlite3/base.py
中sql_flush
方法的源代码,有一条注释说:
Note: No requirement for reset of auto-incremented indices (cf. other sql_flush() implementations). Just return SQL at this point
我在一些测试中加载了依赖于绝对主键id的fixture文件。因为Django没有重置SQLite的autoid字段,所以这些fixture不能正确加载。在
在sqlite中重置auto-id列似乎有点简单:How can I reset a autoincrement sequence number in sqlite
您可以按如下方式对
sql_flush
进行monkey-patch,以重置SQLite序列:例如在
^{pr2}$TransactionTestCase
中使用它:这可以确保依赖于固定主键的测试同时适用于SQLite和其他数据库后端(如PostgreSQL)。但是,有关
reset_sequences
的注意事项,请参见Django documentation。一方面,它使测试变慢。在也许这个片段可以帮助:
相关问题 更多 >
编程相关推荐