SQLAlchemy seeder.
Project description
sqlalchemyseed
Installation
pip install sqlalchemyseed
Dependencies
- SQAlchemy>=1.4.0
- jsonschema>=3.2.0
Getting Started
# main.py
from tests.db import session # import where your session is located.
from sqlalchemyseed import load_entities_from_json, Seeder, HybridSeeder
# load entities
entities = load_entities_from_json("data.json")
# use seeder if you are limited to using 'data' field or you do not query relationship from database
seeder = Seeder()
seeder.seed(entities, session) # session, optional, automatically add entities to session
# or
seeder.seed(entities)
session.add_all(seeder.instances)
# HybridSeeder to use 'filter' field, querying and assigning relationship that exist in the database
seeder = HybridSeeder(session)
seeder.seed(entities)
# for confirmation,
# you can check the added objects by printing session.new and session.dirty
print(session.new)
print(session.dirty)
session.commit()
No Relationship
// data.json
[
{
"model": "models.Person",
"data": {
"name": "You",
"age": 18
}
},
// when you have two or more objects of the same model, you can
{
"model": "models.Person",
"data": [
{
"name": "You",
"age": 18
},
{
"name": "Still You But Older",
"age": 40
}
]
}
]
Relationships
One to One
// data.json
[
{
"model": "models.Person",
"data": {
"name": "John",
"age": 18,
// creates a job object
"job": {
"model": "models.Job",
"data": {
"job_name": "Programmer",
}
}
}
},
// or this, if you want to add relationship that exist
// in your database use 'filter' instead of 'data'
{
"model": "models.Person",
"data": {
"name": "Jeniffer",
"age": 18,
"job": {
"model": "models.Job",
"filter": {
"job_name": "Programmer",
}
}
}
}
]
One to Many
//data.json
[
{
"model": "models.Person",
"data": {
"name": "John",
"age": 18,
"items": [
{
"model": "models.Item",
"data": {
"name": "Pencil"
}
},
{
"model": "models.Item",
"data": {
"name": "Eraser"
}
}
]
}
}
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sqlalchemyseed-0.2.2.tar.gz
(5.0 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemyseed-0.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e25eeaf0837d0539c20d20c465ca8965d920919680e3aa28c61a28ff775b1ab |
|
MD5 | 047f371b35343ee9f7d37ceec9a2597d |
|
BLAKE2b-256 | 0c0c79dd8a888c9920000d71da7fabf1e3d3bceea14e20a594c4d8e379675eda |