一个具有不同数据类型的字段[SQLAlchemy] [英] one field with different data types [SQLAlchemy]

查看:107
本文介绍了一个具有不同数据类型的字段[SQLAlchemy]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值可以是整数,浮点数或字符串,我创建了不同的列:

  #declarative 
class MyClass(Base)

#id和其他列
_value_str =列(String(100))
_value_int =列(整数)
_value_float =列(Float)

def __init __(self,...,value):
self._value_str = value if isinstance(value,(str,unicode))else None
self._value_int = value if isinstance(value,int)else None
self._value_float = value if isinstance(value,float)else None

,我想做这样的事情:

 >> ; value ='text'
>>> my_class = MyClass(value,...)
>>> my_class.value
>>> 'text'
>>> session.add(my_class)
>>> session.commit()
#specialy this
>>> result = session.query(Myclass).filter(Myclass.value =='text')。one()
>>> print result.value
>>> 'text'

也许我有一个设计问题,我接受任何好主意



我是SQLAlchemy中的新朋友



谢谢

解决方案

可能是一个设计问题 - 你的数据库和Python之间的一个不匹配。在SQL变量(列)中有一个类型,而在python值中有类型。



一种可能是使用单个列(字符串)存储之前的价值。



这可以通过 sqlalchemy自定义类型



沿着以下行的某些东西(使用 jsonpickle 进行转换而不是cpickle):

  import sqlalchemy.types as types 
import jsonpickle
from copy import deepcopy

class JsonType(types.MutableType,types.TypeDecorator):
impl = types.Unicode

def process_bind_param(self,value,engine):
return unicode(jsonpickle.encode(value))

def process_result_value ,value,engine):
如果值:
rv = jsonpickle.decode(value)
return rv
else:
return无

def copy_value(self,value):
return deepcopy(value)

def compare_values(self,x,y):
return x == y

然后使用它如下:

 code> class MyClass(base):
value =列(JsonType())


I have a value that can be integer, float or string, and I created different columns:

#declarative
class MyClass(Base):

    #id and other Columns
    _value_str = Column(String(100))
    _value_int = Column(Integer)
    _value_float = Column(Float)

    def __init__(self,...,value):
        self._value_str = value if isinstance(value,(str,unicode)) else None
        self._value_int = value if isinstance(value,int) else None
        self._value_float = value if isinstance(value,float) else None

and i would like to do something like this:

>>> value = 'text'
>>> my_class = MyClass(value, ...)
>>> my_class.value
>>> 'text'
>>> session.add(my_class)
>>> session.commit()
#specialy this
>>> result = session.query(Myclass).filter(Myclass.value == 'text').one()
>>> print result.value 
>>> 'text'

Maybe i have a design problem, i accept any good idea

I'm newbe in SQLAlchemy

Thanks

解决方案

Probably a design problem - a bit of a mismatch between your DB and Python. In SQL variables (columns) have a type, whereas in python values have the type.

One possibility would be to use a single column (a string), but pickle the value before you store it.

This can be accomplished automatically with a sqlalchemy custom type.

Something along the lines of the following (uses jsonpickle to do the conversion rather than cpickle):

import sqlalchemy.types as types
import jsonpickle
from copy import deepcopy

class JsonType(types.MutableType, types.TypeDecorator):    
    impl = types.Unicode

    def process_bind_param(self, value, engine):
        return unicode(jsonpickle.encode(value))

    def process_result_value(self, value, engine):
        if value:
            rv = jsonpickle.decode(value)
            return rv
        else:
            return None

    def copy_value(self, value):
        return deepcopy(value)

    def compare_values(self, x, y):
        return x == y

And then use it as follows:

class MyClass(base):
    value = Column(JsonType())

这篇关于一个具有不同数据类型的字段[SQLAlchemy]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆