Replies: 6 comments
-
|
In general, you can use any column type from class MetricBase(SQLModel):
id: Optional[int] = Field(default=None, primary_key=True)
fact_name: str
dimensions: Optional[List] = Field(default_factory=list, sa_column=Column(ARRAY(String)))
measures: Optional[List] = Field(default_factory=list, sa_column=Column(ARRAY(String)))
params: Optional[Dict] = Field(default_factory=dict, sa_column=Column(JSON)) |
Beta Was this translation helpful? Give feedback.
-
|
Hello thanks, I am actually testing it now... |
Beta Was this translation helpful? Give feedback.
-
|
This will cause confusion between the DB and your API (if you're using works: metric = Metric()
metric.fact_name = "test"
metric.dimensions = ["a", "b", "c"] # works!
metric.measures = [4, 5, 6]
metric.params = {"foo": "bar"}
session.add(metric)
session.commit()not works: app = FastAPI()
@app.post("/")
async def test_post(metric: Metric):
return metricPOST /
{
"id": 0,
"fact_name": "test",
"dimensions": [
"a",
"b",
"c"
],
"measures": [
4,
5,
6
],
"params": {
"foo": "bar"
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Hello, thanks this makes total sense now. |
Beta Was this translation helpful? Give feedback.
-
|
Responding to my own question I get this error:
|
Beta Was this translation helpful? Give feedback.
-
|
You can find all conversions between python types and sqlalchemy types here: It's preferred to use default_factory when you're dealing with mutable types. |
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
It would be nice to show a few examples about how to model arrays and json SQL columns.
In general what principles should I follow to convert from a SQLAlchemy Table definition?
Operating System
Linux
Operating System Details
Ubuntu 21.0
SQLModel Version
0.0.8
Python Version
3.8.10
Additional Context
For example I am trying to convert this existing table:
Beta Was this translation helpful? Give feedback.
All reactions