Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'UUID'

My table is

   categories = table('categories',
                       Column('uuid', UUID(), default=uuid.uuid4,
                              primary_key=True,
                              unique=True, autoincrement=False),
                       Column('name', String),
                       Column('parent', String),
                       Column('created_on', sa.types.DateTime(timezone=True),
                              default=datetime.utcnow())
    )

when I try to insert data, I see

sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'UUID' 'INSERT INTO categories (uuid, name, parent, created_on) VALUES (%(uuid)s, %(name)s, %(parent)s, %(created_on)s)' ({'created_on': datetime.datetime(2013, 3, 31, 4, 12, 26, 801940), 'name': 'Alcohol & Bars', 'parent': 'Food & Drink', 'uuid': UUID('860e5bae-b770-425f-8672-c15c49508a1f')}, {'created_on': datetime.datetime(2013, 3, 31, 4, 12, 26, 801940), 'name': 'Coffee & Tea', 'parent': 'Food & Drink', 'uuid': UUID('de6ad60e-a076-483d-90e9-93916c537583')},

then I change my table to

categories = table('categories',
                   Column('uuid', UUID(), default=str(uuid.uuid4()),
                          primary_key=True,
                          unique=True, autoincrement=False),
                   Column('name', String),
                   Column('parent', String),
                   Column('created_on', sa.types.DateTime(timezone=True),
                          default=datetime.utcnow())
)

then I start to see error as

sqlalchemy.exc.IntegrityError: (IntegrityError) duplicate key value violates unique constraint "categories_pkey"

    DETAIL:  Key (uuid)=(cb80a166-adce-4a6e-9e1a-c210d9b86732) already exists.
     'INSERT INTO categories (uuid, name, parent, created_on) VALUES (%(uuid)s, %(name)s, %(parent)s, %(created_on)s)' ({'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Alcohol & Bars', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Coffee & Tea', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Dessert', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Fast Food', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Groceries', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Other', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Restaurants', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Snacks', 'parent': 'Food & Drink', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}  ... displaying 10 of 43 total bound parameter sets ...  {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Other', 'parent': 'Financial', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'}, {'created_on': datetime.datetime(2013, 3, 31, 4, 14, 22, 732107), 'name': 'Tax Preparation', 'parent': 'Financial', 'uuid': 'cb80a166-adce-4a6e-9e1a-c210d9b86732'})

Now how come uuid.uuid4() is giving the same value always? How can I fix this issue?

Thanks

like image 838
daydreamer Avatar asked Mar 31 '13 04:03

daydreamer


1 Answers

For your first code example to work, you need to set UUID(as_uuid=True) (see the docs on UUID), otherwise DBAPI adapter is expecting a string value.

Your second example doesn't work because default=str(uuid.uuid4()) stores a string value which is generated once, when the table object is defined, and is used each time (hence the duplicate value exception). You need to pass a function here, so it would be evaluated every time a row is inserted:

default=lambda: str(uuid.uuid4())

The same stands for default=datetime.utcnow(), it needs to be default=datetime.utcnow. Another thing is that unique=True constraint is not needed for a primary key, as it's already guaranteed to be unique.

like image 182
Audrius Kažukauskas Avatar answered Nov 01 '22 23:11

Audrius Kažukauskas