Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Decoupling data schema from sql schema (pydantic integration) #502

Open
Labels
@dmsfabiano

Description

@dmsfabiano

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 toPydantic.
  • I already checked if it is not related to SQLModel but toSQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

fromsqlmodelimportfield,SQLModelfrompydanticimportBaseModel"""I want to define these separately, and have one of them be extended by the other"""UserDataSchema(BaseModel):# Name for comparison""" Data model, not tied to the database (i.e. sql) itself can be re-used"""user_id:intproject_id:id# How can this inherit UserDataSchema without re-definition?UserModel(SQLModel,table=True):# Name for comparison""" Data model, not tied to the database (i.e. sql) itself can be re-used"""user_id:Optional[int]=Field(default=None,foreign_key="user.id")project_id:Optional[int]=Field(default=None,foreign_key="project.id")

Description

The issue at hand is that I am not seeing a way from the docs to decouple the data schema from the database schema. Say I have a large platform, with multiple libraries and services. In such case, if we have a static data schema (like our use case), its very valuable to define the data schema in place (sayschema.py as below:

UserDataSchema(BaseModel): # Name for comparison""" Data model, not tied to the database (i.e. sql) itself can be re-used"""   user_id: int   project_id: id

The problem, is that I am not seeing a way to seamlessly translate from thepydantic.BaseModel to the standardSQLModel without having to re-define the entire schema and basically not re-using anything (other than perhaps some functions from the parent class)

I think SQL Alchemy has done it gracefully with their integration ofattrs anddataclassesshere. Which would look ""in theory"", like this

from sqlalchemy import Table, Column, Integer, ForeignKeyUser(SQLModel, UserDataSchema, table=True): # Name for comparison   __table__  = Table(         Column("user_id", Integer, ForeignKey("user.id"), primary_key=True),         Column("project_id", Integer, ForeignKey("project.id"), primary_key=True),)

Am I missing something? is there a straight forward to accomplish something along these lines? Based on the current docs, the only way to do it would be with:

class UserDataSchema(BaseModel):    user_id: int    project_id: intclass User(SQLModel, UserDataSchema, table=True):    user_id: Optional[int] = Field(default=None, primary_key=True)    project_id: Optional[int] = Field(default=None, primary_key=True)

However, that defeats the purpose as we have to redefine each attribute again.

Operating System

Windows

Operating System Details

No response

SQLModel Version

0.0.8

Python Version

3.8

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp