sqlalchemy
adjust_through_many_to_many_model(model_field)
Registers m2m relation on through model. Sets ormar.ForeignKey from through model to both child and parent models. Sets sqlalchemy.ForeignKey to both child and parent models. Sets pydantic fields with child and parent model types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_field |
ManyToManyField
|
relation field defined in parent model |
required |
Source code in ormar\models\helpers\sqlalchemy.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
check_for_null_type_columns_from_forward_refs(config)
Check is any column is of NUllType() meaning it's empty column from ForwardRef
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
OrmarConfig
|
OrmarConfig of the Model without sqlalchemy table constructed |
required |
Returns:
Type | Description |
---|---|
bool
|
result of the check |
Source code in ormar\models\helpers\sqlalchemy.py
260 261 262 263 264 265 266 267 268 269 270 271 |
|
check_pk_column_validity(field_name, field, pkname)
Receives the field marked as primary key and verifies if the pkname was not already set (only one allowed per model).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_name |
str
|
name of field |
required |
field |
BaseField
|
ormar.Field |
required |
pkname |
Optional[str]
|
already set pkname |
required |
Returns:
Type | Description |
---|---|
str
|
name of the field that should be set as pkname |
Raises:
Type | Description |
---|---|
ModelDefintionError
|
if pkname already set |
Source code in ormar\models\helpers\sqlalchemy.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
create_and_append_m2m_fk(model, model_field, field_name)
Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model.
Newly created field is added to m2m relation through model OrmarConfig columns and table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_name |
str
|
name of the column to create |
required |
model |
Type[Model]
|
Model class to which FK should be created |
required |
model_field |
ManyToManyField
|
field with ManyToMany relation |
required |
Source code in ormar\models\helpers\sqlalchemy.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
populate_config_sqlalchemy_table_if_required(config)
Constructs sqlalchemy table out of columns and parameters set on OrmarConfig. It populates name, metadata, columns and constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
OrmarConfig
|
OrmarConfig of the Model without sqlalchemy table constructed |
required |
Source code in ormar\models\helpers\sqlalchemy.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
populate_config_tablename_columns_and_pk(name, new_model)
Sets Model tablename if it's not already set in OrmarConfig. Default tablename if not present is class name lower + s (i.e. Bed becomes -> beds)
Checks if Model's OrmarConfig have pkname and columns set. If not calls the sqlalchemy_columns_from_model_fields to populate columns from ormar.fields definitions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the current Model |
required |
new_model |
Type[Model]
|
currently constructed Model |
required |
Returns:
Type | Description |
---|---|
ormar.models.metaclass.ModelMetaclass
|
Model with populated pkname and columns in OrmarConfig |
Raises:
Type | Description |
---|---|
ModelDefinitionError
|
if pkname is not present raises ModelDefinitionError. Each model has to have pk. |
Source code in ormar\models\helpers\sqlalchemy.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
set_constraint_names(config)
Populates the names on IndexColumns and UniqueColumns and CheckColumns constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
OrmarConfig
|
OrmarConfig of the Model without sqlalchemy table constructed |
required |
Source code in ormar\models\helpers\sqlalchemy.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
sqlalchemy_columns_from_model_fields(model_fields, new_model)
Iterates over declared on Model model fields and extracts fields that should be treated as database fields.
If the model is empty it sets mandatory id field as primary key (used in through models in m2m relations).
Triggers a validation of relation_names in relation fields. If multiple fields are leading to the same related model only one can have empty related_name param. Also related_names have to be unique.
Trigger validation of primary_key - only one and required pk can be set
Sets owner
on each model_field as reference to newly created Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_fields |
Dict
|
dictionary of declared ormar model fields |
required |
new_model |
Type[Model]
|
|
required |
Returns:
Type | Description |
---|---|
Tuple[Optional[str], List[sqlalchemy.Column]]
|
pkname, list of sqlalchemy columns |
Raises:
Type | Description |
---|---|
ModelDefinitionError
|
if validation of related_names fail, or pkname validation fails. |
Source code in ormar\models\helpers\sqlalchemy.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
update_column_definition(model, field)
Updates a column with a new type column based on updated parameters in FK fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Union[Type[Model], Type[NewBaseModel]]
|
model on which columns needs to be updated |
required |
field |
ForeignKeyField
|
field with column definition that requires update |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in ormar\models\helpers\sqlalchemy.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|