Skip to content

models.helpers.sqlalchemy

adjust_through_many_to_many_model

1
adjust_through_many_to_many_model(model_field: "ManyToManyField") -> None

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.

Arguments:

  • model_field: relation field defined in parent model :type model_field: ManyToManyField

create_and_append_m2m_fk

1
create_and_append_m2m_fk(model: Type["Model"], model_field: "ManyToManyField", field_name: str) -> None

Registers sqlalchemy Column with sqlalchemy.ForeignKey leading to the model.

Newly created field is added to m2m relation through model Meta columns and table.

Arguments:

  • field_name: name of the column to create :type field_name: str
  • model: Model class to which FK should be created :type model: Model class
  • model_field: field with ManyToMany relation :type model_field: ManyToManyField field

check_pk_column_validity

1
check_pk_column_validity(field_name: str, field: "BaseField", pkname: Optional[str]) -> Optional[str]

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.

Raises:

  • ModelDefintionError: if pkname already set or field is pydantic_only

Arguments:

  • field_name: name of field :type field_name: str
  • field: ormar.Field :type field: BaseField
  • pkname: already set pkname :type pkname: Optional[str]

Returns:

name of the field that should be set as pkname :rtype: str

sqlalchemy_columns_from_model_fields

1
sqlalchemy_columns_from_model_fields(model_fields: Dict, new_model: Type["Model"]) -> Tuple[Optional[str], List[sqlalchemy.Column]]

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.

Raises:

  • ModelDefinitionError: if validation of related_names fail, or pkname validation fails.

Arguments:

  • model_fields: dictionary of declared ormar model fields :type model_fields: Dict[str, ormar.Field]
  • new_model: :type new_model: Model class

Returns:

pkname, list of sqlalchemy columns :rtype: Tuple[Optional[str], List[sqlalchemy.Column]]

_process_fields

1
_process_fields(model_fields: Dict, new_model: Type["Model"]) -> Tuple[Optional[str], List[sqlalchemy.Column]]

Helper method.

Populates pkname and columns. 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.

Raises:

  • ModelDefinitionError: if validation of related_names fail, or pkname validation fails.

Arguments:

  • model_fields: dictionary of declared ormar model fields :type model_fields: Dict[str, ormar.Field]
  • new_model: :type new_model: Model class

Returns:

pkname, list of sqlalchemy columns :rtype: Tuple[Optional[str], List[sqlalchemy.Column]]

_is_through_model_not_set

1
_is_through_model_not_set(field: "BaseField") -> bool

Alias to if check that verifies if through model was created.

Arguments:

  • field: field to check :type field: "BaseField"

Returns:

result of the check :rtype: bool

_is_db_field

1
_is_db_field(field: "BaseField") -> bool

Alias to if check that verifies if field should be included in database.

Arguments:

  • field: field to check :type field: "BaseField"

Returns:

result of the check :rtype: bool

populate_meta_tablename_columns_and_pk

1
populate_meta_tablename_columns_and_pk(name: str, new_model: Type["Model"]) -> Type["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.

Raises:

  • ModelDefinitionError: if pkname is not present raises ModelDefinitionError. Each model has to have pk.

Arguments:

  • name: name of the current Model :type name: str
  • new_model: currently constructed Model :type new_model: ormar.models.metaclass.ModelMetaclass

Returns:

Model with populated pkname and columns in Meta :rtype: ormar.models.metaclass.ModelMetaclass

check_for_null_type_columns_from_forward_refs

1
check_for_null_type_columns_from_forward_refs(meta: "ModelMeta") -> bool

Check is any column is of NUllType() meaning it's empty column from ForwardRef

Arguments:

  • meta: Meta class of the Model without sqlalchemy table constructed :type meta: Model class Meta

Returns:

result of the check :rtype: bool

populate_meta_sqlalchemy_table_if_required

1
populate_meta_sqlalchemy_table_if_required(meta: "ModelMeta") -> None

Constructs sqlalchemy table out of columns and parameters set on Meta class. It populates name, metadata, columns and constraints.

Arguments:

  • meta: Meta class of the Model without sqlalchemy table constructed :type meta: Model class Meta

set_constraint_names

1
set_constraint_names(meta: "ModelMeta") -> None

Populates the names on IndexColumn and UniqueColumns constraints.

Arguments:

  • meta: Meta class of the Model without sqlalchemy table constructed :type meta: Model class Meta

update_column_definition

1
update_column_definition(model: Union[Type["Model"], Type["NewBaseModel"]], field: "ForeignKeyField") -> None

Updates a column with a new type column based on updated parameters in FK fields.

Arguments:

  • model: model on which columns needs to be updated :type model: Type["Model"]
  • field: field with column definition that requires update :type field: ForeignKeyField

Returns:

None :rtype: None