我正在 Flask
中开发一个项目并使用 SQLAlchemy
作为 ORM
我有一个 User
模型作为
class User(UserMixin, db.Model):
__tablename__ = 'users'
# noinspection PyShadowingBuiltins
uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True,
unique=True)
email = Column('email', String, nullable=False, unique=True)
_password = Column('password', String, nullable=False)
created_on = Column('created_on', sa.types.DateTime(timezone=True),
default=datetime.utcnow())
last_login = Column('last_login', sa.types.DateTime(timezone=True),
onupdate=datetime.utcnow())
- 我使用 Alembic 创建了一个迁移脚本.
- 我选择的数据库是
PostgreSQL
但为了测试我的模型,我更喜欢使用SQLite
来加快测试速度 - 当
PostgreSQL
中有某些类型而不是SQLite
中时出现问题,在本例中为UUID
- 当我在
sqlite://
上运行迁移时,出现错误
AttributeError: 'module' object has no attribute 'GUID'
问题
- 如何使用 GUID
保留我的架构并使用 SQLite 运行内存中测试?好像 SQLite
没有类似的东西
- 当数据库是 PostgreSQL
最佳答案
为什么 SQLite 的测试速度更快?
不管怎样,如果你想把东西保存在内存中……好吧,所以把你的数据库保存在内存中。 其实PostgreSQL会把经常访问的数据移到内存中,但是无论如何,你可以安装一些RAMFS/RAMHDD解决方案并将你的数据库放在那里,所以速度会非常快
你可以谷歌搜索“postgresql on ramdisk”的解决方案
关于python:如何使用内存数据库测试我的数据库模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703386/