Ove*_*ked 11 sqlite sqlalchemy
我试图使用SQLAlchemy声明一个表.我想在表格中包含一个BIGINT自动递增主键.这似乎不适用于sqlite作为数据库后端.另一方面,使用INTEGER自动递增主键可以正常工作.
我读到sqlite的ROWID是签名的bigint.但是有没有办法拥有BIGINT自动增量字段?这样我就可以交换后端,而不必担心db特定问题(假设MySQL和Postgres支持bigint自动递增字段).
谢谢.
hon*_*ane 15
对于通过Google访问并且只需要解决方案的其他人,我编写了以下代码:
# SQLAlchemy does not map BigInt to Int by default on the sqlite dialect.
# It should, but it doesnt.
from sqlalchemy import BigInteger
from sqlalchemy.dialects import postgresql, mysql, sqlite
BigIntegerType = BigInteger()
BigIntegerType = BigIntegerType.with_variant(postgresql.BIGINT(), 'postgresql')
BigIntegerType = BigIntegerType.with_variant(mysql.BIGINT(), 'mysql')
BigIntegerType = BigIntegerType.with_variant(sqlite.INTEGER(), 'sqlite')
Run Code Online (Sandbox Code Playgroud)
这将允许您在数据库上使用BIGINT,并在运行单元测试时使用INT.
ale*_*cxe 11
Sqlite不允许BIGINT用作具有自动增量的主键.
但是,由于sqlite列类型的动态特性,您可以创建特定于后端的列类型并INTEGER在sqlite后端使用类型,请参阅SQLAlchemy:如何根据其后端有条件地为列选择类型.
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
3680 次 |
| 最近记录: |