Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Issue with UUID columns when using Databases
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
You can’t perform that action at this time.