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.

Generate SQLModel table class from dictionary

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

`class CRFForm(CRFFormBase, table=True):
    patient_id: Optional[uuid.UUID] = Field(default_factory=uuid.uuid4,
                                        primary_key=True, nullable=False,
                                        title="Generate a random UUID4 - PK")
    patient_name: str = Field(nullable=False, title='patient_name',
                              description='patient_name', minlength=10, maxlength=50)
    age: int = Field(nullable=False, title='age', description='age', le=120)
    is_married: Optional[bool] = Field(default=True, title="is_married")`

Description

Given the following input (for say).

input_dict = { "table_name": "CRFForm", "columns": [ { "name": "patient_id", "title": "", "description": "", "type": uuid.UUID, "default": uuid.uuid4(), "nullable": False, "primary": True }, { "name": "patient_name", "title": "", "description": "", "type": str, "minlength": 10, "maxlength": 50, "nullable": False, "unique": True }, { "name": "age", "title": "", "description": "", "type": int, "nullable": False, "le": 120 }, { "name": "is_married", "title": "", "description": "", "type": bool, "nullable": False } ] }

Wanted Solution

output —> Generate SQLModel class from the above dictionary by satisfying the table relations and constraints

Wanted Code

`class CRFForm(CRFFormBase, table=True):
    patient_id: Optional[uuid.UUID] = Field(default_factory=uuid.uuid4,
                                        primary_key=True, nullable=False,
                                        title="Generate a random UUID4 - PK")
    patient_name: str = Field(nullable=False, title='patient_name',
                              description='patient_name', minlength=10, maxlength=50)
    age: int = Field(nullable=False, title='age', description='age', le=120)
    is_married: Optional[bool] = Field(default=True, title="is_married")`

Alternatives

No response

Operating System

Linux, Windows, macOS

Operating System Details

Windows 10 Laptop and tianglo 3.9 slimbust image

SQLModel Version

0.8

Python Version

3.9

Additional Context

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
meirdevcommented, Nov 29, 2022

All arguments of the Field function can be defined:

{
    "name": "patient_name",
    "type": str,
    "nullable": False,
    "min_length": 10,
    "max_length": 50,
},
{
    "name": "age",
    "type": int,
    "nullable": False,
    "le": 120,
}

To support foreign key relationships, you should continue with this idea with the Relationship function.

0reactions
Udayaprasadcommented, Dec 16, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a Table with SQLModel - Use the Engine
The first thing we need to do is create a class to represent the data in the table. A class like this that...
Read more >
How to import a Pydantic model into SQLModel?
With that, SQLModel.metadata.create_all(engine) should create the corresponding database table according to your field definitions.
Read more >
Describing Databases with MetaData
Accessing Tables and Columns; Creating and Dropping Database Tables; Altering Database ... To represent a table, use the Table class.
Read more >
Handling SQL Databases With PyQt: The Basics - Real Python
Relational databases are generally organized into a set of tables, ... In PyQt, you can create a database connection by using the QSqlDatabase...
Read more >
Is it possible to instantiate a SQLModel object with ... - GitHub
... from pydantic import BaseModel class Team(SQLModel, table=True): id: ... and have SQLModel correctly create the relationship model based ...
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