Using uuid.UUID with PotgreSQL's UUID
See original GitHub issueI have a model using PostgreSQL’s UUID:
from sqlalchemy.dialects import postgresql
class MyModel(Base):
__tablename__ = 'my-model'
some_id = Column(postgresql.UUID(as_uuid=True), nullable=False)
SQLAlchemy lets me create instances as follows:
import uuid
a_uuid = uuid.uuid4()
MyModel(some_id=a_uuid)
MyModel(some_id=str(a_uuid))
However, the stubs here seem to only accept the latter, not the former:
error: Incompatible type for "some_id" of "MyModel" (got "UUID", expected "str")
Could support for the former be added?
(I tested with sqlalchemy-stubs master)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:12 (10 by maintainers)
Top Results From Across the Web
Generating a UUID in Postgres for Insert statement?
Postgres natively supports UUID as a data type, even capable of being indexed and used as primary key. But to generate a UUID...
Read more >The Basics Of PostgreSQL UUID Data Type
Generating UUID values ... PostgreSQL allows you store and compare UUID values but it does not include functions for generating the UUID values...
Read more >A Complete Guide to UUIDs in PostgreSQL
Universally Unique IDs (UUIDs) use random numbers to create unique IDs. UUIDs use more storage, but they provide a good solution for distributed...
Read more >Documentation: 15: 9.14. UUID Functions
This function returns a version 4 (random) UUID. This is the most commonly used type of UUID and is appropriate for most applications....
Read more >PostgreSQL UUID
The PostgreSQL UUID data type is used to store the UUID values for a specified column. · We can use the CREATE Extension...
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
@wbolster Looks like you answered your own question. I think you can also just use
# type: ignore
on the definition with the explicit annotation. Also I think your way can be simplified to:reveal_type
is handy, but I mean that I don’t understand howsqlalchemy.types.TypeEngine[uuid.UUID]
produces aColumn[UUID*]
. As far as I can tell,TypeEngine
doesn’t even support subscripting. So is this some magic hint tosqlmypy
?(By the way, thanks for sharing the workaround!)