class User(ormar.Model): class Meta(BaseMeta): tablename = "user" id: int = ormar.Integer(primary_key=True, autoincrement=True, nullable=False) user: str = ormar.String( unique=True, index=True, nullable=False, max_length=255 ) # ID of the user on auth0 first: str = ormar.String(nullable=False, max_length=255) last: str = ormar.String(nullable=False, max_length=255) email: str = ormar.String(unique=True, index=True, nullable=False, max_length=255) display_name: str = ormar.String( unique=True, index=True, nullable=False, max_length=255 ) pic_url: str = ormar.Text(nullable=True)
class Filter(ormar.Model): class Meta(ormar.ModelMeta): tablename = "filters" database = database metadata = metadata filter_id = ormar.Integer(primary_key=True, autoincrement=True) name = ormar.String(max_length=200, unique=True, index=True) label = ormar.String(max_length=200) query_text = ormar.Text() allow_multiselect = ormar.Boolean(default=True) created_date = ormar.DateTime(server_default=func.now()) is_dynamic = ormar.Boolean(default=True) is_date = ormar.Boolean(default=False) translation = ormar.ForeignKey(TranslationNode, name="translation_node_id")
class Chart(ormar.Model): class Meta(ormar.ModelMeta): tablename = "charts" database = database metadata = metadata chart_id = ormar.Integer(primary_key=True, autoincrement=True) name = ormar.String(max_length=200, unique=True, index=True) query_text = ormar.Text() datasets = ormar.JSON() layout = ormar.JSON() data_config = ormar.JSON() created_date = ormar.DateTime(server_default=func.now()) library = ormar.String(max_length=200, default="plotly") used_filters = ormar.JSON()
class RandomModel(ormar.Model): class Meta: tablename: str = "random_users" metadata = metadata database = database id: int = ormar.Integer(primary_key=True) password: str = ormar.String(max_length=255, default=gen_pass) first_name: str = ormar.String(max_length=255, default="John") last_name: str = ormar.String(max_length=255) created_date: datetime.datetime = ormar.DateTime( server_default=sqlalchemy.func.now()) @property_field def full_name(self) -> str: return " ".join([self.first_name, self.last_name])
class Session(ormar.Model): class Meta: metadata = metadata database = db tablename = "test_sessions" id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=255, index=True) some_text: str = ormar.Text() some_other_text: Optional[str] = ormar.Text(nullable=True) teacher: Optional[User] = ormar.ForeignKey(User, nullable=True, related_name="teaching") students: Optional[List[User]] = ormar.ManyToMany(User, through=Signup, related_name="attending")
class Car2(ormar.Model): class Meta: abstract = True metadata = metadata database = db id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=50) owner: Person = ormar.ForeignKey(Person, related_name="owned") co_owners: List[Person] = ormar.ManyToMany( Person, # through=PersonsCar, related_name="coowned", ) created_date: datetime.datetime = ormar.DateTime( default=datetime.datetime.now)
class MenuElement(ormar.Model): class Meta(BaseMeta): pass id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) iconClass: str = ormar.String(max_length=200, default="fab fa-chrome") url: str = ormar.String(max_length=200) user: Optional[User] = ormar.ForeignKey(User) def model_to_dict(self): return { "title": self.title, "iconClass": self.iconClass, "url": self.url }
def 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. :param model_fields: dictionary of declared ormar model fields :type model_fields: Dict[str, ormar.Field] :param new_model: :type new_model: Model class :return: pkname, list of sqlalchemy columns :rtype: Tuple[Optional[str], List[sqlalchemy.Column]] """ if len(model_fields.keys()) == 0: model_fields["id"] = ormar.Integer(name="id", primary_key=True) logging.warning("Table {table_name} had no fields so auto " "Integer primary key named `id` created.") validate_related_names_in_relations(model_fields, new_model) columns = [] pkname = None for field_name, field in model_fields.items(): field.owner = new_model if field.is_multi and not field.through: field.create_default_through_model() if field.primary_key: pkname = check_pk_column_validity(field_name, field, pkname) if not field.pydantic_only and not field.virtual and not field.is_multi: columns.append(field.get_column(field.get_alias())) return pkname, columns
class Genre(ormar.Model): """ `genres` table mapping. Attributes ---------- id: str, primary key artist: Artist, foreign key genre: str """ class Meta(BaseMeta): pass id: int = ormar.Integer(primary_key=True, autoincrement=True) artist: Artist = ormar.ForeignKey(Artist) genre: str = ormar.Text()
class TrackArtist(ormar.Model): """ `tracks_artists` table mapping. Attributes ---------- id: str, primary key artist: Artist, foreign key track: Track, foreign key """ class Meta(BaseMeta): pass id: str = ormar.Integer(primary_key=True, autoincrement=True) artist: Artist = ormar.ForeignKey(Artist) track: Track = ormar.ForeignKey(Track)
class UserToken(ormar.Model): """ `user_tokens` table mapping. Attributes ---------- id: int, primary key user: User, foreign key access_token: str """ class Meta(BaseMeta): pass id: str = ormar.Integer(primary_key=True, autoincrement=True) user: User = ormar.ForeignKey(User) access_token: Optional[str] = ormar.Text(nullable=True)
class ExampleModel(Model): class Meta: tablename = "example" metadata = metadata test: int = ormar.Integer(primary_key=True) test_string: str = ormar.String(max_length=250) test_text: str = ormar.Text(default="") test_bool: bool = ormar.Boolean(nullable=False) test_float: ormar.Float() = None # type: ignore test_datetime = ormar.DateTime(default=datetime.datetime.now) test_date = ormar.Date(default=datetime.date.today) test_time = ormar.Time(default=datetime.time) test_json = ormar.JSON(default={}) test_bigint: int = ormar.BigInteger(default=0) test_decimal = ormar.Decimal(scale=2, precision=10) test_decimal2 = ormar.Decimal(max_digits=10, decimal_places=2)
class Meeting(ormar.Model): """ Describe `Meeting` model in database """ class Meta(BaseMeta): # pylint:disable=(missing-class-docstring) pass id: int = ormar.Integer(primary_key=True) meeting_id: str = ormar.String(max_length=70) title: str = ormar.String(max_length=70) calendar_id: str = ormar.String(max_length=150, nullable=True) event_id: str = ormar.String(max_length=150, nullable=True) course_code: str = ormar.String(max_length=150, nullable=True) start_url: str = ormar.String(max_length=1000) join_url: str = ormar.String(max_length=100) is_active: bool = ormar.Boolean(default=True) created: datetime = ormar.DateTime(default=datetime.now()) email: LicenseAccount = ormar.ForeignKey(LicenseAccount)
class PlayedTrack(ormar.Model): """ `played_tracks` table mapping. Attributes ---------- id: str, primary key user: User, foreign key track: Track, foreign key played_at: datetime """ class Meta(BaseMeta): pass id: int = ormar.Integer(primary_key=True, autoincrement=True) user: User = ormar.ForeignKey(User) track: Track = ormar.ForeignKey(Track) played_at: datetime = ormar.DateTime()
class Artist(ormar.Model): """ `artists` table mapping. Attributes ---------- id: str, primary key name: str href: str popularity: int, optional """ class Meta(BaseMeta): pass id: str = ormar.Text(primary_key=True) name: str = ormar.Text() href: str = ormar.Text() uri: str = ormar.Text() popularity: int = ormar.Integer(nullable=True)
class Truck2(Car2): class Meta: tablename = "trucks2" max_capacity: int = ormar.Integer()
class Truck(Car): class Meta: pass max_capacity: int = ormar.Integer()
class Category(ormar.Model): class Meta(LocalMeta): tablename = "categories" id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100)
class PriceList(ormar.Model): class Meta(BaseMeta): tablename = "price_lists" id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100)
class JsonSample3(ormar.Model): class Meta: tablename = "jsons3" id: int = ormar.Integer(primary_key=True) test_json = ormar.JSON(nullable=True)
class Category(ormar.Model): class Meta(MainMeta): pass id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100)
class DataSource(ormar.Model): class Meta(BaseMeta): tablename = "datasources" id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=200, unique=True, index=True)
class PostCategory2(ormar.Model): class Meta(BaseMeta): tablename = "posts_x_categories2" id: int = ormar.Integer(primary_key=True) sort_order: int = ormar.Integer(nullable=True)
class UserLike(ormar.Model): class Meta(MainMata): pass id: int = ormar.Integer(primary_key=True)
class User(BaseModel): id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) class Meta(BaseMeta): tablename = "users"
class Bus2(Car2): class Meta: tablename = "buses2" max_persons: int = ormar.Integer()
class Bus3(Car2): # pragma: no cover class Meta: tablename = "buses3" owner: Person = ormar.ForeignKey(Person, related_name="buses") max_persons: int = ormar.Integer()
class Category(ormar.Model): class Meta(BaseMeta): tablename = "categories" id = ormar.Integer(primary_key=True) name = ormar.String(max_length=40)
class Author(ormar.Model): class Meta(BaseMeta): tablename = "authors" id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100)
class Blog(ormar.Model): class Meta(BaseMeta): pass id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200)