Using hybrid_property of SQLAlchemy leads to CompileError
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
from sqlalchemy.ext.hybrid import hybrid_property
from typing import Optional
from sqlmodel import SQLModel, Field, create_engine
from datetime import datetime
class ObjectTable(SQLModel, table=True):
object_id: Optional[int] = Field(primary_key=True, default=None)
first_detection_time: datetime = Field(index=True)
last_detection_time: datetime = Field(index=True)
@hybrid_property
def detection_time(self) -> float:
return (self.last_detection_time - self.first_detection_time).total_seconds()
class Config:
arbitrary_types_allowed = True
if __name__ == "__main__":
engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine)
Description
I am trying to create a hybrid property in an SQLModel class to allow more complex querying. Following the steps as described in the sqlalchemy docs here: https://docs.sqlalchemy.org/en/14/orm/extensions/hybrid.html I assumed that this would work and create a valid table. However, this code gives the error:
sqlalchemy.exc.CompileError: (in table 'objecttable', column detection_time'): Can't generate DDL for NullType(); did you forget to specify a type on this Column?
At first, I assumed that a type hint was missing so I added the float
return type to the hybrid_property
. I am not sure what the problem is exactly but I assumed that this code would yield a valid table.
Operating System
Linux
Operating System Details
Ubuntu 20.04
SQLModel Version
0.0.6
Python Version
Python 3.8.10
Additional Context
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:6
Top Results From Across the Web
Hybrid Attributes - SQLAlchemy 1.4 Documentation
Below, each function decorated with hybrid_method or hybrid_property may receive self as an instance of the class, or as the class itself:.
Read more >SQLAlchemy - using hybrid properties in a query
1 Answer 1 ; import func class Metric ; Base): __tablename__ = 'metric' id ; True) value = Column(Float, nullable=False) @hybrid_property ...
Read more >sqlalchemy Changelog - pyup.io
key is actually present, and is not instead using "None" due to the ... Fixed regression in hybrid_property where a hybrid against a...
Read more >Google Maps and Excel download - oCoCarbon
SQLAlchemy hybrid property for QuickBooks deletions ... “Compile error: The code in this project must be updated for use on 64-bit systems.
Read more >How to implement SQL level expression for this hybrid property?
to sqlalchemy. I have a ledger table and a corresponding python class. I defined the model using SQLAlchemy, as follows, class Ledger(Base): __tablename__ ......
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
Nope. @tiangolo that would be really cool if you can take a look into it.
Any update regarding this issue?
My model:
On 0.0.8 I’ve got an error: