question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How create a ModelMixin?

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
byrmancommented, May 5, 2022

Indeed, a pure mixin does not appear to work, but perhaps you can do this as a workaround:

class ModelMixin(SQLModel):
    pass


class Phone(ModelMixin, table=True):
    pass
1reaction
byrmancommented, May 5, 2022

Didn’t you fix this in https://github.com/tiangolo/sqlmodel/pull/256/files ?

That fix works fine for methods and simple attributes, but unfortunately not for model fields, which have to be processed by metaclass machinery. I didn’t realise that until I tried this use case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Reusable Models with Django and Mixins
Mixins can provide a ton of functionality to not just your database models, but other classes you might have as well. Using them...
Read more >
Django Model Mixins: inherit from models.Model or from object?
Mixins inherit from model.Model but are configured as an abstract class. · Because mixins inherit from model.Model , your actual model should not ......
Read more >
Use Mixins to create flexible models in Django - DeHaat
Mixin is a concept that aims to solve the problem of multiple inheritances. In brief, it removes the duplicate logic in different classes...
Read more >
Using mixins with class-based views - Django documentation
Building up Django's generic class-based views​​ There are also mixins involved in the generic edit views ( FormView , and the model-specific views...
Read more >
Model mixins - Django Design Patterns and Best Practices
Model mixins are abstract classes that can be added as a parent class of a model. Python supports multiple inheritances, unlike other languages...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found