Flask-admin: Error when Primary Key is UUID

Created on 3 Mar 2017  路  3Comments  路  Source: flask-admin/flask-admin

Thanks for awesome work on Flask-Admin.

I believe there is error/bug in actual app. With model and admin:

from sqlalchemy.dialects.postgresql.base import UUID
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
db.UUID = UUID

class ProductType(db.Model):
    id = db.Column(db.UUID, default=uuid.uuid4, primary_key=True)
    description = db.Column(db.String(50), nullable=False)

admin.add_view(ModelView(ProductType, db.session))

When we create new ProductType we got error:

Failed to create record. (builtins.AttributeError) 'UUID' object has no attribute 'replace' [SQL: 'INSERT INTO product_type (id, description) VALUES (%(id)s, %(description)s)'] [parameters: [{'description': 'test'}]]

screen shot 2017-03-03 at 22 17 13

I can create failing TestCase PR for that, so it could be maybe patched?

Most helpful comment

This was issue on my side. Note for everyone in future.

Model should be

class ProductType(db.Model):
    id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
    description = db.Column(db.String(50), nullable=False)

this argument as_uuid is really important (db.UUID(as_uuid=True)).

All 3 comments

This was issue on my side. Note for everyone in future.

Model should be

class ProductType(db.Model):
    id = db.Column(db.UUID(as_uuid=True), default=uuid.uuid4, primary_key=True)
    description = db.Column(db.String(50), nullable=False)

this argument as_uuid is really important (db.UUID(as_uuid=True)).

What is uuid in :
default=uuid.uuid4 ?

you have to import it- import uuid

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fwiersENO picture fwiersENO  路  6Comments

MaasErwin picture MaasErwin  路  7Comments

pmazurek picture pmazurek  路  6Comments

macfire picture macfire  路  3Comments

lucasvo picture lucasvo  路  4Comments