Last active
August 21, 2019 07:29
-
-
Save magnusahlberg/b1469f29bf37676181bfcdc8c4fb8b22 to your computer and use it in GitHub Desktop.
Issue with UUID columns when using Databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from sqlalchemy import MetaData, Table, Column, Float | |
from sqlalchemy.dialects.postgresql import UUID | |
from databases import Database | |
database = Database("postgresql://test@127.0.0.1/test") | |
metadata = MetaData() | |
activities = Table( | |
"activities", | |
metadata, | |
Column("id", UUID(as_uuid=False), primary_key=True), # Results in: [{'id': UUID('348beab0-34dc-11e7-8762-1b7a0b79ec0c'), 'points': 6.667}] | |
# Column("id", UUID(as_uuid=True), primary_key=True), # Results in: AttributeError: 'UUID' object has no attribute 'replace' | |
Column("points", Float), | |
) | |
async def run_query(): | |
await database.connect() | |
query = activities.select(limit=1) | |
rows = await database.fetch_all(query=query) | |
content = [ | |
{"id": row[activities.c.id], "points": row[activities.c.points]} for row in rows | |
] | |
return content | |
if __name__ == "__main__": | |
json = asyncio.run(run_query()) | |
print(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment