No project description provided
Project description
sqlalchemyseed
SQLAlchemy seeder. One-to-one, one-to-many, many-to-many relationship support.
Installation
pip install sqlalchemyseed
Dependencies
- SQAlchemy>=1.4.0
Getting Started
# main.py
from db import session # import where your session is located.
from sqlalchemyseed import create_objects
from sqlalchemyseed import load_entities_from_json
entities = load_entities_from_json("data.json")
# create_objects returns the objects created, while automatically added them to the session
# auto_add, add to session every time an object is created, useful when you have 'filter' field
objects = create_objects(entities, session, auto_add=True)
# you can check the added objects by printing session.new
print(session.new)
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
}
]
}
]
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.0.2.tar.gz
(3.7 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemyseed-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 622b0237f6ca4c8138b484eb59acff6e1b30b70288a683574162595900c67254 |
|
MD5 | 0f4978a2f4847c22b9a25d7af7f4501a |
|
BLAKE2b-256 | 53d77b62ed8e69db4f431e69c050403dce5ad48be670726aec5cb8b55fc51341 |