Replies: 28 comments
-
|
Hello @hay-kot , i have tried your solution on nested models, and it works just fine, I will try with more nested pedantic models and see if it works, yet I would greatly appreciate if you could provide some explanation(documentation) of what you did because although I read it many times, I am still afraid to use it for production. |
Beta Was this translation helpful? Give feedback.
-
|
SQLModel is fantastic as it cuts so much time out managing pydantic schemas and sqlalchemy models. I too would like to know how to create database entries with incoming nested SQLModels. Does anyone know how to do this yet? |
Beta Was this translation helpful? Give feedback.
-
|
It's even worse than use separate pydantic and sqlalchemy models because the nested list on the payload received from client are always empty. So it's not even possible to create each element separarely |
Beta Was this translation helpful? Give feedback.
-
|
Agree with @hay-kot : it would be great to have the ability to instantiate nested objects from a nested SQLModel instance. |
Beta Was this translation helpful? Give feedback.
-
|
This would such an amazing feature. Any update on this? |
Beta Was this translation helpful? Give feedback.
-
|
Any update on this ? |
Beta Was this translation helpful? Give feedback.
-
|
This is a major feature that is missing from sqlmodel that is preventing myself from migrating a larger project from marshmallow. There is a lot of boilerplate code that needs to be added to handle nested objects |
Beta Was this translation helpful? Give feedback.
-
|
I would also love to see this addition to the awesome SQLModel library. |
Beta Was this translation helpful? Give feedback.
-
|
Started working with this wonderful library and encountered the same problem. Any updates? |
Beta Was this translation helpful? Give feedback.
-
|
+1 on this. First of all, this library looks amazing! Last year, I was using Sqlalchemy and Pydantic models separately, but this library would be such a quality of life improvement.. Unfortunately, the fact that this feature is missing is a deal breaker, so i cannot start using it. Last year, I also built a custom recursive solution to do this in Sqlalchemy+Pydantic, just like OP did. But, I don't think it would be easy/a good idea to port it to SqlModels... Any updates would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
-
|
Any update on this? Would love to use |
Beta Was this translation helpful? Give feedback.
-
|
There any temporary guidance from best practices to avoid this issue? |
Beta Was this translation helpful? Give feedback.
-
|
any updates on it? this is something that will be amazing for SQLModel to have. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, is there any updates or fixes for this? |
Beta Was this translation helpful? Give feedback.
-
|
Still facing these issues is there any updates about? |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
-
|
+1 @tiangolo any updates for this feature. This is a great library but would love to have this feature. |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
-
|
This makes it quite difficult to use SQLModel in LLM applications, as we need to build out our models from JSON output from an LLM. There doesn't seem to be an obviously supported way to do this right now. |
Beta Was this translation helpful? Give feedback.
-
|
I ended up inheriting my class BaseTable(SQLModel):
__abstract__ = True
id: Optional[int] = Field(default=None, primary_key=True)
# your other default columns / fields / methods here
@classmethod
def from_json(cls, json_str: str) -> Self:
return cls.from_dict(json.loads(json_str))
@classmethod
def from_dict(cls, data: dict) -> Self:
values = {}
for field_name, raw_value in data.items():
if field_name in {"id", "created_at", "updated_at"}:
continue
field_value = cls._build_field(field_name, raw_value)
if field_value is None and raw_value is not None: # skipped value
continue
values[field_name] = field_value
return cls(**values)
@classmethod
def _build_field(cls, field_name: str, raw_value: Any) -> Any:
if field_name in cls.model_fields:
return raw_value
rel = cls.__mapper__.relationships.get(field_name)
if not rel:
return None
is_belongs_to_rel = f"{field_name}_id" in cls.model_fields
if is_belongs_to_rel:
return None
FieldModel = rel.mapper.class_
if rel.uselist:
sub_items = []
for item in raw_value:
if isinstance(item, dict):
sub_items.append(FieldModel.from_dict(item))
elif isinstance(item, FieldModel):
sub_items.append(item)
else:
continue
return sub_items
if isinstance(raw_value, dict):
return FieldModel.from_dict(raw_value)
if isinstance(raw_value, FieldModel):
return raw_value
return None |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I would like to do what is described in FastAPI issue #2194
In the example above, I'd like to pass in the payload to a method and the following to occur.
Similarly, I'd like the same to happen on update. Effectively making writing to the SQL database akin to writing to MongoDB
I don't believe this is supported or haven't gotten it to work, but my main questions are.
Loving working with this so far, thanks for all your hard work!
Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.3
Python Version
3.9.6
Additional Context
I have accomplished this with SQLAlchemy in the past by using an
auto_initdecarator.Usage
Beta Was this translation helpful? Give feedback.
All reactions