Question

I am using the SQlAlchemy recommended uuid() support add-on as described here. However, when I use it in my SQLAlchemy code, I receive this error:

TypeError: 'module' object is not callable

Referring to the module, GUID.

Here is the GUID code, taken directly from the source:

GUID.py

from sqlalchemy.types import TypeDecorator, CHAR
from sqlalchemy.dialects.postgresql import UUID
import uuid

class GUID(TypeDecorator):
  """Platform-independent GUID type.

  Uses Postgresql's UUID type, otherwise uses
  CHAR(32), storing as stringified hex values.

  """
  impl = CHAR

  def load_dialect_impl(self, dialect):
    if dialect.name == 'postgresql':
      return dialect.type_descriptor(UUID())
    else:
      return dialect.type_descriptor(CHAR(32))

  def process_bind_param(self, value, dialect):
    if value is None:
      return value
    elif dialect.name == 'postgresql':
      return str(value)
    else:
      if not isinstance(value, uuid.UUID):
        return "%.32x" % uuid.UUID(value)
      else:
        # hexstring
        return "%.32x" % value

  def process_result_value(self, value, dialect):
    if value is None:
      return value
    else:
      return uuid.UUID(value)

And here is my model which calls it

user.py

from app import db
from datetime import datetime
from app.custom_db import GUID

class User(db.Model):
  __tablename__ = 'users'
  id = db.Column(GUID(), primary_key=True)
  email = db.Column(db.String(80), unique=True)
  name = db.Column(db.String(80))
  password = db.Column(db.String(80))
  datejoined = db.Column(db.DateTime,default = db.func.now())

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

  def __repr__(self):
    return '<User %r>' % self.name

Any idea why I can't create this uuid() PKey?


Here's the full traceback

Traceback (most recent call last):                                                                                                                                                                                                            
  File "./run.py", line 3, in <module>                                                                                                                                                                                                        
    from app import app                                                                                                                                                                                                                       
  File "/home/achumbley/Pile/app/__init__.py", line 23, in <module>                                                                                                                                                                           
    from models import user                                                                                                                                                                                                                   
  File "/home/achumbley/Pile/app/models/user.py", line 5, in <module>                                                                                                                                                                         
    class User(db.Model):                                                                                                                                                                                                                     
  File "/home/achumbley/Pile/app/models/user.py", line 7, in User                                                                                                                                                                             
    id = db.Column(GUID(), primary_key=True)                                                                                                                                                                                                  
TypeError: 'module' object is not callable 
Was it helpful?

Solution

If your file is GUID.py, and you're importing it like from app.custom_db import GUID (as you have it), then what you've really imported is the file, not the class. To get to the class, you would need to call GUID.GUID().

Alternatively, you could import the class by importing it as:

from app.custom_db.GUID import GUID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top