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.

Using uuid.UUID with PotgreSQL's UUID

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

8reactions
ilevkivskyicommented, Aug 22, 2019

@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:

if TYPE_CHECKING:
    PostgreSQLUUID = sqlalchemy.types.TypeEngine[uuid.UUID]
else:
    PostgreSQLUUID = sqlalchemy.dialects.postgresql.UUID(as_uuid=True)
3reactions
sirosencommented, Jul 16, 2020

reveal_type is handy, but I mean that I don’t understand how sqlalchemy.types.TypeEngine[uuid.UUID] produces a Column[UUID*]. As far as I can tell, TypeEngine doesn’t even support subscripting. So is this some magic hint to sqlmypy?

(By the way, thanks for sharing the workaround!)

Read more comments on GitHub >

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

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