SQLModelX
SQLModelX is an extension of the SQLModel library.
Install
pip install sqlmodelx
Usage
from datetime import datetime
from typing import List,Optional
from sqlmodel import Field, Relationship, Session, select
from sqlmodelx import SQLModel
from sqlmodelx.main import SQLModelMetaclass
class PkMixin(SQLModel):
id: Optional[int] = Field(default = None, primary_key = True, nullable = False)
class BaseUser(PkMixin):
username: str = Field(default = '', nullable = False)
password: str = Field(default = '', nullable = False)
create_time: datetime = Field(default_factory = datetime.now, nullable = False)
group_id: Optional[int] = Field(default = None, nullable = True, foreign_key = 'group.id')
class User(BaseUser, table = True):
__tablename__ = 'user'
group: Optional['Group'] = Relationship(back_populates = 'users')
class Group(SQLModel, table = True):
id: Optional[int] = Field(default = None, primary_key = True, nullable = False)
name: str = Field(default = '', nullable = False)
create_time: datetime = Field(default_factory = datetime.now, nullable = False)
users: List[User] = Relationship(
back_populates = 'group',
sa_relationship_kwargs = {"enable_typechecks": False}
)
def test_class_and_metaclass(engine):
"""Test class and metaclass"""
from sqlmodel import SQLModel as _SQLModel
from sqlmodel.main import SQLModelMetaclass as _SQLModelMetaclass
assert isinstance(User, SQLModelMetaclass)
assert isinstance(User, _SQLModelMetaclass)
assert issubclass(User, SQLModel)
assert issubclass(User, _SQLModel)
def test_base_is_table_and_subclass_is_table(engine):
"""Test base class and subclass are both ORM database tables"""
# Extend the user ORM model to add a field
class NickNameUser(User, table = True):
nickname: str = Field(default = '')
# Extend the user ORM model to add a field
class AvatarUser(NickNameUser, table = True):
avatar: str = Field(default = '')
# Create the database tables
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)
avatar_user = AvatarUser(
username = "Deadpond",
password = "Dive Wilson",
nickname = 'nickname',
avatar = 'avatar',
group = Group(name = 'admin'),
)
with Session(engine) as session:
session.add(avatar_user)
session.commit()
session.refresh(avatar_user)
assert avatar_user.id is not None
# The relationship property of the base class will also be inherited
assert avatar_user.group.id is not None
nickname_user = session.query(NickNameUser).first()
assert nickname_user.nickname == avatar_user.nickname
# The relationship property of the base class will also be inherited
assert nickname_user.group.id == avatar_user.group.id
user = session.exec(select(User)).first()
assert user.username == avatar_user.username
assert user.group.id == avatar_user.group.id
def test_base_is_table_and_subclass_is_not_table(engine):
"""Test base class is an ORM database table, the subclass is not"""
# Create a pydantic model quickly through inheritance
class NickNameUserSchema(User, table = False):
nickname: str = Field(default = '')
user = User(
username = "Deadpond",
password = "Dive Wilson",
group = Group(name = 'admin')
)
with Session(engine) as session:
session.add(user)
session.commit()
session.refresh(user)
assert user.id is not None
assert user.group.id is not None
user_ex = NickNameUserSchema.from_orm(user)
assert user_ex.id == user.id
License
According to the Apache2.0 protocol.
Metadata
Release files for sqlmodelx 0.0.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqlmodelx-0.0.13.tar.gz | 11.8 kB | Without provenance | Details for sqlmodelx-0.0.13.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqlmodelx-0.0.13-py3-none-any.whl | Python 3 | none | any | Details for sqlmodelx-0.0.13-py3-none-any.whl |
Total release size: 20.6 kB
Release files / sqlmodelx-0.0.13.tar.gz
| Download URL | sqlmodelx-0.0.13.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
16cc9d3de6324c882c77bdff0ae0e0156d85626a2d6160f78e3c4e680e8b9f4c
|
|
BLAKE2b-256 checksum How to use checksums |
9e5e40fcf8b1551bc7333f99b2ac7a5cc35f1b68c86282a2073ce878e0af0915
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.16.1 CPython/3.11.0 Windows/10
|
Release files / sqlmodelx-0.0.13-py3-none-any.whl
| Download URL | sqlmodelx-0.0.13-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ed0c562026ce381a3395652943323bd913dfc83a95eee7e5d2c9e507180f0dc
|
|
BLAKE2b-256 checksum How to use checksums |
c860786a89ba48f0eba0913f3c451200513729442b5c15805ce3e1ba85f6eb01
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.16.1 CPython/3.11.0 Windows/10
|
Security for sqlmodelx 0.0.13
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.