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.

How to create computed columns ?

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 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:open
  • Created 2 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

10reactions
dandiepcommented, Dec 8, 2021

Any other thoughts on how to actually use the computed_property() from SQL alchemy? E.g.

class Parent(SQLModel):
....
    child_count: Optional[int] = Field(sa_column=column_property(
        select(func.count(Child.id)).where(Child.parent_id == id)
    ))

This results in in “column Parent.child_count does not exist.”

0reactions
angel-langdoncommented, Feb 10, 2022
Read more comments on GitHub >

github_iconTop 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 >

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