SQLAlchemy flush()和commit()SQLAlchemy 之间有什么区别?
我已经阅读了文档,但没有更明智 - 他们似乎假设我没有预先理解.
我对它们对内存使用的影响特别感兴趣.我正在从一系列文件(总共约500万行)中将一些数据加载到数据库中,并且我的会话偶尔会崩溃 - 它是一个大型数据库和一台内存不足的机器.
我想知道我是否使用了太多但commit()没有足够的flush()电话 - 但是如果没有真正了解其中的差异,那就很难说了!
当我尝试创建数据库模式迁移时,我遇到了这个奇怪的错误.你能帮我弄清楚出了什么问题吗?
$ python app.py db upgrade
[skipped]
sqlalchemy.exc.ArgumentError: Mapper Mapper|EssayStateAssociations|essay_associations could not assemble any primary key columns for mapped table 'essay_associations'
Run Code Online (Sandbox Code Playgroud)
我的模特:
class EssayStateAssociations(db.Model):
__tablename__ = 'essay_associations'
application_essay_id = db.Column(
db.Integer,
db.ForeignKey("application_essay.id"),
primary_key=True),
theme_essay_id = db.Column(
db.Integer,
db.ForeignKey("theme_essay.id"),
primary_key=True),
state = db.Column(db.String, default="pending")
Run Code Online (Sandbox Code Playgroud) 我正在使用Alembic和SQLAlchemy进行项目,但是在测试中无法在数据库中创建简单条目。我收到以下错误:
sqlalchemy.exc.ArgumentError: Mapper Mapper|Sale|sales_cache could not assemble any primary key columns for mapped table 'sales_cache'
Run Code Online (Sandbox Code Playgroud)
我已经在下面的两个地方都建立了主键(account_id),你知道为什么SQLAlchemy无法识别该主键或如何解决它吗?我读过的其他答案都处理了多个主键/无主键的异常情况,并已相应解决,但这是一个失败的漂亮模型。
我已经阅读了其他答案,其中大多数涉及声明式系统:
class Sale(Base):
__tablename__ = 'sales_cache'
Run Code Online (Sandbox Code Playgroud)
但是我必须使用经典的制图系统。这分别是我的映射类和架构:
class Sale(object):
def __init__(self, notification):
self._sale_id = self._notification.object_id
self._account_id = self._notification.account_id
### schema.py file ###
from sqlalchemy.schema import MetaData, Table, Column
from sqlalchemy.types import (Unicode, Integer)
from database import metadata
metadata = MetaData()
sales_cache = Table('sales_cache', metadata,
Column('account_id', Integer, primary_key=True, autoincrement=False),
Column('sale_id', Integer, nullable=False)
)
Run Code Online (Sandbox Code Playgroud)
这是我的Alembic修订中的相关内容:
sa.Column('account_id', sa.Integer(), primary_key=True, autoincrement=False),
Run Code Online (Sandbox Code Playgroud)
我以为它可能会失败,因为我正在设置self._sale_id和self._account_id而不是self.sale_id和 …