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
52
```python
56
-
57
53
from sqlalchemyseed import HybridSeeder
58
-
fromtests.db import session
54
+
from db import session
59
55
60
56
# Assuming that Child(age=5) exists in the database or session,
61
-
# then we should use 'filter' instead of 'obj'
57
+
# then we should use 'filter' instead of 'data'
62
58
# the the values of 'filter' will query from the database or session, and assign it to the Parent.child
63
59
data = {
64
60
"model": "models.Parent",
@@ -77,9 +73,57 @@ data = {
77
73
seeder = HybridSeeder(session)
78
74
seeder.seed(data)
79
75
76
+
session.commit() # or seeder.sesssion.commit()
80
77
```
81
78
82
-
## No Relationship
79
+
## Relationships
80
+
81
+
In adding a relationship attribute, add prefix '!' to the key in order to identify it.
82
+
83
+
### Referencing relationship object or a foreign key
84
+
85
+
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
86
+
87
+
```python
88
+
from sqlalchemyseed import HybridSeeder
89
+
from db import session
90
+
91
+
instance = [
92
+
{
93
+
'model': 'tests.models.Company',
94
+
'data': {'name': 'MyCompany'}
95
+
},
96
+
{
97
+
'model': 'tests.models.Employee',
98
+
'data':[
99
+
{
100
+
'name': 'John Smith',
101
+
# foreign key attribute
102
+
'!company_id': {
103
+
'model': 'tests.models.Company',
104
+
'filter': {
105
+
'name': 'MyCompany'
106
+
}
107
+
}
108
+
},
109
+
{
110
+
'name': 'Juan Dela Cruz',
111
+
# relationship attribute
112
+
'!company': {
113
+
'model': 'tests.models.Company',
114
+
'filter': {
115
+
'name': 'MyCompany'
116
+
}
117
+
}
118
+
]
119
+
}
120
+
]
121
+
122
+
seeder = HybridSeeder(session)
123
+
seeder.seed(instance)
124
+
```
125
+
126
+
### No Relationship
83
127
84
128
```json5
85
129
// test_data.json
@@ -108,10 +152,6 @@ seeder.seed(data)
108
152
]
109
153
```
110
154
111
-
## Relationships
112
-
113
-
In adding a relationship attribute, add prefix '!' to the key in order to identify it.
114
-
115
155
### One to One
116
156
117
157
```json5
@@ -178,7 +218,7 @@ In adding a relationship attribute, add prefix '!' to the key in order to identi
178
218
]
179
219
```
180
220
181
-
## Example of Nested Relationships
221
+
### Example of Nested Relationships
182
222
183
223
```json
184
224
{
@@ -203,5 +243,4 @@ In adding a relationship attribute, add prefix '!' to the key in order to identi
0 commit comments