SQLAlchemy seeder.
Project description
sqlalchemyseed
Sqlalchemy seeder. Supports nested relationships.
Installation
pip install sqlalchemyseed
Dependencies
- SQAlchemy>=1.4.0
Getting Started
# main.py
from sqlalchemyseed import load_entities_from_json, Seeder
from tests.db import session
# load entities
entities = load_entities_from_json('tests/test_data.json')
# Initializing Seeder
seeder = Seeder() # or Seeder(session)
# Seeding
seeder.session = session # assign session if no session assigned before seeding
seeder.seed(entities)
# Committing
session.commit() # or seeder.session.commit()
Seeder vs. HybridSeeder
- Seeder supports model and data key fields.
- Seeder does not support model and filter key fields.
- Seeder when seeding, can specify to not add all entities to session by passing this argument
add_to_session=False
in theSeeder.seed
method. - HybridSeeder supports 'model' and 'data', and 'model' and 'filter' key fields.
- HybridSeeder enables to query existing objects from the session and assigns it with the relationship.
- HybridSeeder when seeding, automatically adds all entities to session.
When to use HybridSeeder and 'filter' key field?
from sqlalchemyseed import HybridSeeder
from tests.db import session
# Assuming that Child(age=5) exists in the database or session,
# then we should use 'filter' instead of 'obj'
# the the values of 'filter' will query from the database or session, and assign it to the Parent.child
data = {
"model": "models.Parent",
"data": {
"!child": {
"model": "models.Child",
"filter": {
"age": 5
}
}
}
}
# When seeding instances that has 'filter' key, then use HybridSeeder, otherwise use Seeder.
seeder = HybridSeeder(session)
seeder.seed(data)
No Relationship
// test_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
In adding a relationship attribute, add prefix '!' to the key in order to identify it.
One to One
// test_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 exists
// in your database use 'filter' instead of 'obj'
{
"model": "models.Person",
"data": {
"name": "Jeniffer",
"age": 18,
"!job": {
"model": "models.Job",
"filter": {
"job_name": "Programmer",
}
}
}
}
]
One to Many
//test_data.json
[
{
"model": "models.Person",
"data": {
"name": "John",
"age": 18,
"!items": [
{
"model": "models.Item",
"data": {
"name": "Pencil"
}
},
{
"model": "models.Item",
"data": {
"name": "Eraser"
}
}
]
}
}
]
Example of Nested Relationships
{
"model": "models.Parent",
"data": {
"name": "John Smith",
"!children": [
{
"model": "models.Child",
"data": {
"name": "Mark Smith",
"!children": [
{
"model": "models.GrandChild",
"data": {
"name": "Alice Smith"
}
}
]
}
}
]
}
}
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.3.2.tar.gz
(6.0 kB
view hashes)
Built Distribution
Close
Hashes for sqlalchemyseed-0.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a712ee0aac9e077cdc0381873abed22f6961e25e9fb173b903c6ef92dfb6cdc |
|
MD5 | 4fcb4b4b55921e6352c9e6e2b512f15d |
|
BLAKE2b-256 | cf532954ae928f47c70220a2380b2cd7d6667b85c12f917d78500a6b9f329bd2 |