如何使用sqlalchemy将初始数据加载到数据库中

时间:2021-12-27 03:05:14

I want to be able to load data automatically upon creation of tables using SQLAlchemy.

我希望能够在使用SQLAlchemy创建表时自动加载数据。

In django, you have fixtures which allow you to easily pre-populate your database with data upon creation of a table. This I found useful especially when you have basic "lookup" tables e.g. product_type, student_type which contain just a few rows or even a table like currencies which will load all the currencies of the world without you having to key them in over and over again when you destroy your models/classes.

在django中,您可以使用fixtures,在创建表时,可以使用数据轻松预填充数据库。我觉得这很有用,特别是当你有基本的“查找”表时,例如product_type,student_type只包含几行甚至是一个像货币一样的表格,它们会加载世界上所有的货币而不必在销毁模型/类时反复键入它们。

My current app isn't using django. I have SQLAlchemy. How can I achieve the same thing? I want the app to know that the database is being created for the first time and hence it populates some tables with data.

我目前的应用程序不使用django。我有SQLAlchemy。我怎样才能达到同样的目的?我希望应用程序知道第一次创建数据库,因此它会使用数据填充一些表。

2 个解决方案

#1


1  

The short answer is no, SQLAlchemy doesn't provide the same feature as dumpdata and loaddata like Django.

简短的回答是否定的,SQLAlchemy没有提供与像Django一样的dumpdata和loaddata相同的功能。

There is https://github.com/kvesteri/sqlalchemy-fixtures that might be useful for you but the workflow is different.

有https://github.com/kvesteri/sqlalchemy-fixtures可能对您有用,但工作流程不同。

#2


1  

I used the event listener to prepopulate database with data upon creation of a table.

我使用事件监听器在创建表时使用数据预填充数据库。

Let's say you have ProductType model in your code:

假设您的代码中有ProductType模型:

class ProductType(Base):
    __tablename__ = 'product_type'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))

First, you need to define a callback function, which will be executed when the table is created:

首先,您需要定义一个回调函数,该函数将在创建表时执行:

def insert_data(target, connection, **kw):
    connection.execute(target.insert(), {'id': 1, 'name':'spam'}, {'id':2, 'name': 'eggs'})

Then you just add the event listener:

然后你只需添加事件监听器:

event.listen(ProductType.__table__, 'after_create', insert_data)

#1


1  

The short answer is no, SQLAlchemy doesn't provide the same feature as dumpdata and loaddata like Django.

简短的回答是否定的,SQLAlchemy没有提供与像Django一样的dumpdata和loaddata相同的功能。

There is https://github.com/kvesteri/sqlalchemy-fixtures that might be useful for you but the workflow is different.

有https://github.com/kvesteri/sqlalchemy-fixtures可能对您有用,但工作流程不同。

#2


1  

I used the event listener to prepopulate database with data upon creation of a table.

我使用事件监听器在创建表时使用数据预填充数据库。

Let's say you have ProductType model in your code:

假设您的代码中有ProductType模型:

class ProductType(Base):
    __tablename__ = 'product_type'
    id = Column(Integer, primary_key=True)
    name = Column(String(100))

First, you need to define a callback function, which will be executed when the table is created:

首先,您需要定义一个回调函数,该函数将在创建表时执行:

def insert_data(target, connection, **kw):
    connection.execute(target.insert(), {'id': 1, 'name':'spam'}, {'id':2, 'name': 'eggs'})

Then you just add the event listener:

然后你只需添加事件监听器:

event.listen(ProductType.__table__, 'after_create', insert_data)