class Meta: database = database metadata = metadata # define your constraints in Meta class of the model # it's a list that can contain multiple constraints # hera a combination of name and column will have to be unique in db constraints = [ormar.UniqueColumns("name", "completed")]
def update_attrs_from_base_meta( # noqa: CCR001 base_class: "Model", attrs: Dict, model_fields: Dict ) -> None: """ Updates Meta parameters in child from parent if needed. :param base_class: one of the parent classes :type base_class: Model or model parent class :param attrs: new namespace for class being constructed :type attrs: Dict :param model_fields: ormar fields in defined in current class :type model_fields: Dict[str, BaseField] """ params_to_update = ["metadata", "database", "constraints"] for param in params_to_update: current_value = attrs.get("Meta", {}).__dict__.get(param, ormar.Undefined) parent_value = ( base_class.Meta.__dict__.get(param) if hasattr(base_class, "Meta") else None ) if parent_value: if param == "constraints": verify_constraint_names( base_class=base_class, model_fields=model_fields, parent_value=parent_value, ) parent_value = [ ormar.UniqueColumns(*x._pending_colargs) for x in parent_value ] if isinstance(current_value, list): current_value.extend(parent_value) else: setattr(attrs["Meta"], param, parent_value)
class Meta: abstract = True metadata = metadata database = db constraints = [ ormar.UniqueColumns("creation_date", "modification_date") ]
def _build_model_meta( table: Table, metadata: MetaData, database: Database ) -> Type[ormar.ModelMeta]: # constraints constraints = [] for const in table.constraints: if isinstance(const, sqlalchemy.UniqueConstraint): constraints.append( ormar.UniqueColumns(*const._pending_colargs) # type: ignore ) Meta = type( "Meta", (ormar.ModelMeta,), { "metadata": metadata, "database": database, "tablename": table.key, "constraints": constraints, }, ) return cast(Type[ormar.ModelMeta], Meta)
class Meta(ormar.ModelMeta): tablename = "categories" constraints = [ormar.UniqueColumns("name", "code")]
class Meta(BaseMeta): tablename = "events" constraints = [ormar.UniqueColumns('code')]
class Meta(BaseMeta): tablename = "admins" constraints = [ormar.UniqueColumns('username')]
class Meta(BaseMeta): tablename = "claims" constraints = [ormar.UniqueColumns('attendee', 'event')]
class Meta: tablename = "products" metadata = metadata database = database constraints = [ormar.UniqueColumns("name", "company")]