Python 枚举列类型的Sqlalchemy模型
我正在尝试将python枚举存储在一个表中。我尝试跟踪stackoverflow帖子,但现在出现以下错误:Python 枚举列类型的Sqlalchemy模型,python,model,sqlalchemy,flask-sqlalchemy,flask-migrate,Python,Model,Sqlalchemy,Flask Sqlalchemy,Flask Migrate,我正在尝试将python枚举存储在一个表中。我尝试跟踪stackoverflow帖子,但现在出现以下错误: File "c:\users\arrchana\pycharmprojects\be\venv\lib\site-packages\sqlalchemy\dialects\postgresql\base.py", line 2231, in format_type raise exc.CompileError("PostgreSQL ENUM type requires a n
File "c:\users\arrchana\pycharmprojects\be\venv\lib\site-packages\sqlalchemy\dialects\postgresql\base.py", line 2231, in format_type
raise exc.CompileError("PostgreSQL ENUM type requires a name.")
sqlalchemy.exc.CompileError: PostgreSQL ENUM type requires a name.
这是我的代码:
from appInits.db import db
from enums.goalTypes import GoalTypes
from sqlalchemy import types
class EnumAsInteger(types.TypeDecorator):
"""Column type for storing Python enums in a database INTEGER column.
This will behave erratically if a database value does not correspond to
a known enum value.
"""
impl = types.Integer # underlying database type
def __init__(self, enum_type):
super(EnumAsInteger, self).__init__()
self.enum_type = enum_type
def process_bind_param(self, value, dialect):
if isinstance(value, self.enum_type):
return value.value
raise ValueError('expected %s value, got %s'
% (self.enum_type.__name__, value.__class__.__name__))
def process_result_value(self, value, dialect):
return self.enum_type(value)
def copy(self, **kwargs):
return EnumAsInteger(self.enum_type)
class Tasks(db.Model):
__tablename__ = 'tasks'
id = db.Column(db.Integer, primary_key=True, unique=True)
text = db.Column(db.Text, unique=True, nullable=False)
difficulty = db.Column(db.Integer, unique=False, nullable=False)
tool = db.Column(EnumAsInteger(GoalTypes), nullable=False)
def __repr__(self):
return '<Task {}: {}, {}, {}>'.format(self.id, self.difficulty, self.tool, self.text)
def __init__(self, id, text, difficulty, tool):
self.id = id
self.text = text
self.difficulty = difficulty
self.tool = tool
从appInits.db导入数据库
从enums.goalTypes导入目标类型
从sqlalchemy导入类型
类EnumAsInteger(类型.TypeDecorator):
“”“用于在数据库整数列中存储Python枚举的列类型。
如果数据库值与
一个已知的枚举值。
"""
impl=types.Integer#基础数据库类型
定义初始化(自身,枚举类型):
超级(EnumAsInteger,self)。\uuuu init
self.enum\u type=枚举类型
def process_bind_参数(自身、值、方言):
如果isinstance(值,self.enum_类型):
返回值
raise VALUERROR('应为%s值,已为%s'
%(self.enum_type._名称_,值._类_._名称_))
def过程\结果\值(自身、值、方言):
返回self.enum_类型(值)
def副本(自身,**kwargs):
返回EnumAsInteger(self.enum\u类型)
类任务(db.Model):
__tablename_uu='tasks'
id=db.Column(db.Integer,主键=True,unique=True)
text=db.Column(db.text,unique=True,nullable=False)
难度=db.Column(db.Integer,unique=False,nullable=False)
tool=db.Column(EnumAsInteger(目标类型),nullable=False)
定义报告(自我):
返回“”。格式(self.id、self.defficiency、self.tool、self.text)
定义初始化(自我、id、文本、难度、工具):
self.id=id
self.text=文本
困难
self.tool=工具
这是栈柱:
请发布完整的堆栈跟踪,而不是代码段。请发布完整的堆栈跟踪,而不是代码段。