psycopg2: Can't adapt type 'UUID'?

后端 未结 2 369
再見小時候
再見小時候 2021-01-12 05:59

I am using psycopg2 to try to insert an entry into a table where the type of the data is the Postgres type \'uuid\'.

According to this page, I should be able to dire

相关标签:
2条回答
  • 2021-01-12 06:24

    As author noted in comments, to pass UUID objects into cursor methods one have to call register_uuid() first once:

    import psycopg2.extras
    
    # call it in any place of your program
    # before working with UUID objects in PostgreSQL
    psycopg2.extras.register_uuid()
    
    # now you can pass UUID objects into psycopg2 functions
    cursor.execute("INSERT INTO MyTable (uuid) VALUES (%s)", (uuid.uuid4(),))
    
    # ... and even get it from there
    cursor.execute("SELECT uuid FROM MyTable")
    value, = cursor.fetchone()
    assert isinstance(value, uuid.UUID)
    
    0 讨论(0)
  • 2021-01-12 06:36
    uuid_entry = str(uuid.uuid4()) 
    

    This works for me. Not sure if it is the right approach.

    0 讨论(0)
提交回复
热议问题