SQLAlchemy seeder.
Project description
sqlalchemyseed
Sqlalchemy seeder. Supports nested relationships.
Installation
pip install sqlalchemyseed
Dependencies
- SQAlchemy>=1.4.0
- jsonschema>=3.2.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')
# Initialize 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.
- 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.
When to use HybridSeeder and 'filter' key field?
from sqlalchemyseed import HybridSeeder
from tests.db import session
instances = {
"model": "models.Parent",
"data": {
"child": {
"model": "models.Child",
"filter": {
"age": 5
}
}
}
}
# Assuming that Child(age=5) exists in the database or session,
# the we can use 'filter'
# When seeding instances that has 'filter' key, then use HybridSeeder, otherwise use Seeder.
seeder = HybridSeeder(session)
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
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 'data'
{
"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
Built Distribution
Close
Hashes for sqlalchemyseed-0.3.0.dev1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf7e4885ae5e32def0cd7a0d8b49d662ad7695a0cea789d3ae50d2c3387fa1dc |
|
MD5 | c8a4f5b0b02c9265f96c9e6c5f880449 |
|
BLAKE2b-256 | 0794fba42c6f22e733e99b4791523398b79fa142a113a5a66c8dfa9e2bca987c |
Close
Hashes for sqlalchemyseed-0.3.0.dev1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a03c29c940a2f5a1559ab72c3d83765d33f3f399c8e2713529a8d3952de6dbdd |
|
MD5 | c2faa953a28e63c06ec669573ffb9525 |
|
BLAKE2b-256 | 90c442ed9ef2658277356194b58d6cf92116590b96ce40a963fbe741eda8c623 |