我刚刚从NoSQL迁移到了SqlAlchemy,而且我对SqlAlchemy ORM相当陌生。
在我的用例中,我需要模型中的一个字段来存储给定的选择集:
# models.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.utils.types.choice import ChoiceType
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
USER_TYPES = [
('user', 'User'),
('admin', 'Admin User')
]
id = Column(Integer(), primary_key=True)
type = Column(ChoiceType(User_Types), default='user')但是当我运行我的脚本时,我得到:
> SAWarning: Unicode column 'None' has non-unicode default value 'user' specified.
self.default在我设置默认值并键入not“ChoiceType”的其他字段上没有错误)。
有人知道我做错了什么吗?
谢谢!
发布于 2015-10-15 17:34:31
默认情况下,ChoiceType列是Unicode(255)。Unicode类型是一个String子类,它假设输入和输出都是Python数据。应该将默认值设置为unicode字符串。
type = Column(ChoiceType(USER_TYPES), default=u'user')也可以设置impl参数。
type = Column(ChoiceType(USER_TYPES, impl=String()), default='user')https://stackoverflow.com/questions/33153965
复制相似问题