How to create computed columns ?
See original GitHub issueFirst 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 Album(SQLModel):
title: str
slug: str
description: str
# what i've tried but doesn't work e.g :
# slug: str = column_property(slugify(title))
# E NameError: name 'title' is not defined
class Album(SQLModel):
title: str
slug: str = column_property(slugify(title))
description: str
Description
I’m trying to generate a column named “slug” that is a slugified version of “title” so it should be persisted in database, and be updated when title changes.
But so far no luck, I’ve looked at column property but didn’t manage to make it work with SQLModel. I saw that there are events “before update” … but I don’t think this is the way to go
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.9.2
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:5
Top Results From Across the Web
Specify Computed Columns in a Table - SQL Server
In Object Explorer, expand the table for which you want to add the new computed column. Right-click Columns and select New Column. Enter...
Read more >An overview of computed columns in SQL Server - SQLShack
A computed column in SQL Server is a virtual column that computes its values from an expression. We can use a constant value,...
Read more >An Essential Guide to SQL Server Computed Columns By ...
First, specify the name of the table to which you want to add the computed column. · Second, specify the computed column name...
Read more >Computed Column Performance in SQL Server - Simple Talk
To define a computed column, specify the column name, followed by the AS keyword and then the expression. In this case, the expression ......
Read more >Computed Columns | CockroachDB Docs
A stored computed column (set with the STORED SQL keyword) is calculated when a row is inserted or updated, and stores the resulting...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Any other thoughts on how to actually use the computed_property() from SQL alchemy? E.g.
This results in in “column Parent.child_count does not exist.”
Related to this issue https://github.com/tiangolo/sqlmodel/issues/240