SQLAlchemy seeder.
Project description
sqlalchemyseed
Sqlalchemy seeder that supports nested relationships.
Installation
Default installation
pip install sqlalchemyseed
When using yaml to loading entities from yaml files. Execute this command to install necessary dependencies
pip install sqlalchemyseed[yaml]
Dependencies
Required
- SQAlchemy>=1.4.0
Optional
- PyYAML>=5.4.0
Getting Started
# main.py
from sqlalchemyseed import load_entities_from_json, Seeder
from 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
Features & Options | Seeder | HybridSeeder |
---|---|---|
Support model and data keys |
:heavy_check_mark: | :heavy_check_mark: |
Support model and filter keys |
:x: | :heavy_check_mark: |
Optional argument add_to_session=False in the seed method |
:heavy_check_mark: | :x: |
Assign existing objects from session or db to a relationship attribute | :x: | :heavy_check_mark: |
When to use HybridSeeder and 'filter' key field?
Assuming that Child(age=5)
exists in the database or session,
then we should use filter instead of data,
the values of filter will query from the database or session,
and assign it to the Parent.child
from sqlalchemyseed import HybridSeeder
from db import session
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)
session.commit() # or seeder.sesssion.commit()
Relationships
In adding a relationship attribute, add prefix ! to the key in order to identify it.
Referencing relationship object or a foreign key
If your class don't have a relationship attribute but instead a foreign key attribute you can use it the same as how you did it on a relationship attribute
from sqlalchemyseed import HybridSeeder
from db import session
instance = [
{
'model': 'tests.models.Company',
'data': {'name': 'MyCompany'}
},
{
'model': 'tests.models.Employee',
'data':[
{
'name': 'John Smith',
# foreign key attribute
'!company_id': {
'model': 'tests.models.Company',
'filter': {
'name': 'MyCompany'
}
}
},
{
'name': 'Juan Dela Cruz',
# relationship attribute
'!company': {
'model': 'tests.models.Company',
'filter': {
'name': 'MyCompany'
}
}
]
}
]
seeder = HybridSeeder(session)
seeder.seed(instance)
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
}
]
}
]
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
Built Distribution
Hashes for sqlalchemyseed-0.4.2.dev2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52e3fe779d1a4172950080ec9d13b994ea0fa9087f3649bfadd212e4d5fb3433 |
|
MD5 | 58a05e22230ec2d608e99e827695ffd5 |
|
BLAKE2b-256 | 3e95f5237d912e467acd945ff6db74d13693d72b52a614bd6ac78da5e9486e8b |
Hashes for sqlalchemyseed-0.4.2.dev2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9c93662b735f74299182d88aa208b63da0660ce033d3a191ed35cf900adebe3 |
|
MD5 | f126fa2901193bbcc4fd624c901bb8a2 |
|
BLAKE2b-256 | 1dd63dce1472fe0ac685407648fe37177675dbf651a1aadc36cca7f1ca47dbf9 |