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(meta)
Check is any column is of NUllType() meaning it's empty column from ForwardRef
Parameters:
Name | Type | Description | Default |
---|---|---|---|
meta |
ModelMeta
|
Meta class of the Model without sqlalchemy table constructed |
required |
Returns:
Type | Description |
---|---|
bool
|
result of the check |
Source code in ormar\models\helpers\sqlalchemy.py
267 268 269 270 271 272 273 274 275 276 277 278 |
|
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) and if field is not marked as pydantic_only as it needs to be a database field.
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 or field is pydantic_only |
Source code in ormar\models\helpers\sqlalchemy.py
96 97 98 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 Meta 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 |
|
populate_meta_sqlalchemy_table_if_required(meta)
Constructs sqlalchemy table out of columns and parameters set on Meta class. It populates name, metadata, columns and constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
meta |
ModelMeta
|
Meta class of the Model without sqlalchemy table constructed |
required |
Source code in ormar\models\helpers\sqlalchemy.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
populate_meta_tablename_columns_and_pk(name, new_model)
Sets Model tablename if it's not already set in Meta. Default tablename if not present is class name lower + s (i.e. Bed becomes -> beds)
Checks if Model's Meta 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 Meta |
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
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 258 259 260 261 262 263 264 |
|
set_constraint_names(meta)
Populates the names on IndexColumns and UniqueColumns and CheckColumns constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
meta |
ModelMeta
|
Meta class of the Model without sqlalchemy table constructed |
required |
Source code in ormar\models\helpers\sqlalchemy.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
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, cannot be pydantic_only.
Append fields to columns if it's not pydantic_only, virtual ForeignKey or ManyToMany field.
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 156 157 158 159 |
|
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
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|