7

I saw this snippet of code from Flask-common at line 57:

id = db.Column(UUID, default=lambda: str(uuid.uuid4()), primary_key=True)

So I thought I give it a try and use it in my app's models.py (because I prefer to have uuid type for my id's)

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import UUID
import uuid

from app import db

class CostCenter(db.Model):
    __tablename__ = "costcenter"

    id = db.Column('id', UUID(as_uuid=True), default=lambda: str(uuid.uuid4()), primary_key=True)
    name = db.Column('name', db.Text)

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return '<id {}>'.format(self.id)

But when I try to run python manage.py db upgrade, it results to an error:

  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 190, in __init__                                                          
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 213, in process                                                           
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch                                                 
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 2164, in visit_create_table                                               
  File "build/bdist.linux-x86_64/egg/sqlalchemy/util/compat.py", line 199, in raise_from_cause                                                   
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 2153, in visit_create_table                                               
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 213, in process                                                           
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch                                                 
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 2184, in visit_create_column                                              
  File "build/bdist.linux-x86_64/egg/sqlalchemy/dialects/sqlite/base.py", line 847, in get_column_specification                                  
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/compiler.py", line 261, in process                                                           
  File "build/bdist.linux-x86_64/egg/sqlalchemy/sql/visitors.py", line 79, in _compiler_dispatch                                                 
sqlalchemy.exc.CompileError: (in table 'costcenter', column 'id'): Compiler <sqlalchemy.dialects.sqlite.base.SQLiteTypeCompiler object at 0x7fc16
c1f1a50> can't render element of type <class 'sqlalchemy.dialects.postgresql.base.UUID'> 

Why can't it render the type UUID? It is recognized in SQLAlchemy but how come it can't in Flask-SQLAlchemy?

3
  • 3
    You can't use a Postgres data type with SQLite. They're two different databases. Commented Apr 23, 2016 at 4:12
  • 1
    For anyone who gets here and is thinking "but I didn't set my database to sqlite!" -- you probably aren't passing in your postgres connection information into sqlalchemy properly, and so it is defaulting to using sqlite as the database. Check your DB configuration information. Commented Aug 9, 2019 at 18:12
  • 2
    Is there an alternative, where depending on the database type you are working with, it runs one line or another? Commented Jan 2, 2020 at 22:19

2 Answers 2

21

Your column definition is using a function that only works with postgresql, and your database type is sqlite.

You need this:

id = db.Column(
  'id', 
  db.Text(length=36), 
  default=lambda: str(uuid.uuid4()), 
  primary_key=True)
Sign up to request clarification or add additional context in comments.

2 Comments

I only read this by now but it so happened I also resorted to a Text column too. Thanks for this though.
Text also isn't available in sqlite? I thought all SQLALchemy().types where utilizable in any kind of db.
0

You can use UUID just in case of using postgresql database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.