Skip to content

How create a ModelMixin? #330

@yurihartmann

Description

@yurihartmann

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from datetime import datetime
from typing import Optional

from sqlalchemy.orm import declared_attr
from sqlmodel import Field, Relationship, SQLModel


class ModelMixin:
    __name__: str
    __config__ = {}

    created_at: datetime = Field(default_factory=datetime.utcnow)
    update_at: datetime = Field(default=datetime.utcnow)
    deleted_at: datetime = Field(default=None, nullable=True)


class Phone(ModelMixin, SQLModel, table=True):

    id: Optional[int] = Field(default=None, primary_key=True)
    cellphone: str
    is_active: Optional[bool] = Field(default=True)

Description

  • Created the ModelMixin with fields
  • Created the model Phone
  • extends ModelMixin in Phone
  • When run code "SQLModel.metadata.create_all(self._engine)" only fields in Phone is created
  • The fields created_at, update_at and deleted_at is ignorated

Operating System

macOS

Operating System Details

macos 12.3.1

SQLModel Version

0.0.6

Python Version

Python 3.9.9

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions