You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Support `model` and `data` keys |:heavy_check_mark:|:heavy_check_mark:|
46
+
| Support `model` and `filter` keys |:x:|:heavy_check_mark:|
47
+
| Optional argument `add_to_session=False` in the `seed` method |:heavy_check_mark:|:x:|
48
+
| Assign existing objects from session or db to a relationship attribute |:x:|:heavy_check_mark:|
52
49
53
50
## When to use HybridSeeder and 'filter' key field?
54
51
55
-
```python
52
+
Assuming that `Child(age=5)` exists in the database or session,
53
+
then we should use *filter* instead of *data*,
54
+
the values of *filter* will query from the database or session,
55
+
and assign it to the `Parent.child`
56
56
57
+
```python
57
58
from sqlalchemyseed import HybridSeeder
58
-
fromtests.db import session
59
+
from db import session
59
60
60
-
# Assuming that Child(age=5) exists in the database or session,
61
-
# then we should use 'filter' instead of 'obj'
62
-
# the the values of 'filter' will query from the database or session, and assign it to the Parent.child
63
61
data = {
64
62
"model": "models.Parent",
65
63
"data": {
@@ -77,9 +75,57 @@ data = {
77
75
seeder = HybridSeeder(session)
78
76
seeder.seed(data)
79
77
78
+
session.commit() # or seeder.sesssion.commit()
79
+
```
80
+
81
+
## Relationships
82
+
83
+
In adding a relationship attribute, add prefix **!** to the key in order to identify it.
84
+
85
+
### Referencing relationship object or a foreign key
86
+
87
+
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
88
+
89
+
```python
90
+
from sqlalchemyseed import HybridSeeder
91
+
from db import session
92
+
93
+
instance = [
94
+
{
95
+
'model': 'tests.models.Company',
96
+
'data': {'name': 'MyCompany'}
97
+
},
98
+
{
99
+
'model': 'tests.models.Employee',
100
+
'data':[
101
+
{
102
+
'name': 'John Smith',
103
+
# foreign key attribute
104
+
'!company_id': {
105
+
'model': 'tests.models.Company',
106
+
'filter': {
107
+
'name': 'MyCompany'
108
+
}
109
+
}
110
+
},
111
+
{
112
+
'name': 'Juan Dela Cruz',
113
+
# relationship attribute
114
+
'!company': {
115
+
'model': 'tests.models.Company',
116
+
'filter': {
117
+
'name': 'MyCompany'
118
+
}
119
+
}
120
+
]
121
+
}
122
+
]
123
+
124
+
seeder = HybridSeeder(session)
125
+
seeder.seed(instance)
80
126
```
81
127
82
-
## No Relationship
128
+
### No Relationship
83
129
84
130
```json5
85
131
// test_data.json
@@ -108,10 +154,6 @@ seeder.seed(data)
108
154
]
109
155
```
110
156
111
-
## Relationships
112
-
113
-
In adding a relationship attribute, add prefix '!' to the key in order to identify it.
114
-
115
157
### One to One
116
158
117
159
```json5
@@ -178,7 +220,7 @@ In adding a relationship attribute, add prefix '!' to the key in order to identi
178
220
]
179
221
```
180
222
181
-
## Example of Nested Relationships
223
+
### Example of Nested Relationships
182
224
183
225
```json
184
226
{
@@ -203,5 +245,4 @@ In adding a relationship attribute, add prefix '!' to the key in order to identi
0 commit comments