2014-02-09 47 views
0

我正在使用SQlAlchemy推薦的uuid()支持插件,如here所述。但是,當我在SQLAlchemy代碼中使用它時,我收到此錯誤:SQLAlchemy'模塊'不可調用

TypeError: 'module' object is not callable 

引用模塊GUID。

這裏是GUID代碼,直接從源頭上採取:

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) 

這裏是我的模型,稱之爲

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 

任何想法,爲什麼我不能創建這個uuid() PKey?


下面是完整的回溯

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 
+0

回溯請。 –

+0

'app/custom_db.py'看起來像什麼?您正在從該文件導入GUID,所以我的猜測是該文件錯誤地導入了GUID。 –

回答

3

如果你的文件GUID.py,而你喜歡from app.custom_db import GUID導入它(因爲你擁有它),然後你才真正進口的是檔案,而不是班級。要參加課程,您需要致電GUID.GUID()

或者,你可以通過導入它作爲導入類:

from app.custom_db.GUID import GUID 
+0

..我覺得啞巴。感謝您發現錯誤! –