使用 SQLAlchemy ORM 批量插入多个表
Bulk insert into multiple tables using SQLAlchemy ORM
我正在尝试使用 Flask-SQLAlchemy 批量插入 2 tables。 2 table 是:
- 作者table:PKauthor_id(自增)
- book table: PK book_id (ai), FK author_author_id
在 JSON 的正文中,我有一个字典列表。每个词典条目都有一些与作者相关的信息和一些与书籍相关的信息。像这样,一下子可以多送很多字典:
[
{
"author_first_name": "John",
"author_last_name": "Doe",
"author_yob": "1988",
"book_name": "Mournings of a nun",
"book_genre": "Drama"
},
{
"author_first_name": "Jane",
"author_last_name": "Doe",
"author_yob": "1987",
"book_name": "Need for speed",
"book_genre": "Action"
}
]
目前,我正在遍历每本词典,将数据插入作者 table,然后插入书籍 table。当我插入 author table 并提交时,我得到一个主键,它是 author_id。那是我的书 table.
的外键
我为列表中的每个条目重复此步骤。有没有办法进行批量插入,这样如果任何插入失败,一切都会回滚,而且我的数据库中没有不一致的数据?因此,如果上面 JSON 中有 15 个词典,如果第 12 个词典有一些无效数据或数据库出现故障,我希望 JSON 中发送的数据的 none 应该发布到 AWS RDS。下面,"results"指的是我上面提到的JSON。
@classmethod
def post_to_database(cls, results):
for result in results:
BookModel.post_entry_to_database(result)
@classmethod
def post_entry_to_database(cls, result):
BookModel.insert_author_entry(result)
author_id = BookModel.get_author_id(result)
BookModel.insert_book_entry(author_id, result)
@classmethod
def insert_book_entry(cls, author_id, result):
book_data = BookModel(result["testname"], result["result"], author_id)
db.session.add(book_data)
db.session.commit()
同样,我也有insert_author_entry。
谢谢,
阿迪亚
您可以考虑使用 flush()
将更改刷新到数据库,这将更新您的主键字段。
来自 SqlAlchemy 文档:flush
Database operations will be issued in the current transactional context and do not affect the state of the transaction, unless an error occurs, in which case the entire transaction is rolled back. You may flush() as often as you like within a transaction to move changes from Python to the database’s transaction buffer.
当调用 flush
时,数据库将您的 CRUD 操作保持在事务中待处理,并且不会永久保留,直到数据库收到当前事务的 COMMIT
。这将在您随后调用 commit()
.
时发生
我正在尝试使用 Flask-SQLAlchemy 批量插入 2 tables。 2 table 是:
- 作者table:PKauthor_id(自增)
- book table: PK book_id (ai), FK author_author_id
在 JSON 的正文中,我有一个字典列表。每个词典条目都有一些与作者相关的信息和一些与书籍相关的信息。像这样,一下子可以多送很多字典:
[
{
"author_first_name": "John",
"author_last_name": "Doe",
"author_yob": "1988",
"book_name": "Mournings of a nun",
"book_genre": "Drama"
},
{
"author_first_name": "Jane",
"author_last_name": "Doe",
"author_yob": "1987",
"book_name": "Need for speed",
"book_genre": "Action"
}
]
目前,我正在遍历每本词典,将数据插入作者 table,然后插入书籍 table。当我插入 author table 并提交时,我得到一个主键,它是 author_id。那是我的书 table.
的外键我为列表中的每个条目重复此步骤。有没有办法进行批量插入,这样如果任何插入失败,一切都会回滚,而且我的数据库中没有不一致的数据?因此,如果上面 JSON 中有 15 个词典,如果第 12 个词典有一些无效数据或数据库出现故障,我希望 JSON 中发送的数据的 none 应该发布到 AWS RDS。下面,"results"指的是我上面提到的JSON。
@classmethod
def post_to_database(cls, results):
for result in results:
BookModel.post_entry_to_database(result)
@classmethod
def post_entry_to_database(cls, result):
BookModel.insert_author_entry(result)
author_id = BookModel.get_author_id(result)
BookModel.insert_book_entry(author_id, result)
@classmethod
def insert_book_entry(cls, author_id, result):
book_data = BookModel(result["testname"], result["result"], author_id)
db.session.add(book_data)
db.session.commit()
同样,我也有insert_author_entry。
谢谢, 阿迪亚
您可以考虑使用 flush()
将更改刷新到数据库,这将更新您的主键字段。
来自 SqlAlchemy 文档:flush
Database operations will be issued in the current transactional context and do not affect the state of the transaction, unless an error occurs, in which case the entire transaction is rolled back. You may flush() as often as you like within a transaction to move changes from Python to the database’s transaction buffer.
当调用 flush
时,数据库将您的 CRUD 操作保持在事务中待处理,并且不会永久保留,直到数据库收到当前事务的 COMMIT
。这将在您随后调用 commit()
.