包含多列的SQLAlchemy自定义类型 [英] SQLAlchemy Custom Type Which Contains Multiple Columns
问题描述
我想将数据类型表示为模型中的单个列,但实际上数据将存储在数据库的多个列中.我在SQLAlchemy中找不到如何执行此操作的任何好的资源.
I would like to represent a datatype as a single column in my model, but really the data will be stored in multiple columns in the database. I cannot find any good resources on how to do this in SQLAlchemy.
我希望我的模型看起来像这样(这是一个使用几何图形的简化示例,而不是我的实际问题,这很难解释):
I would like my model to look like this(this is a simplified example using geometry instead of my real problem which is harder to explain):
class 3DLine(DeclarativeBase):
start_point = Column(my.custom.3DPoint)
end_point = Column(my.custom.3DPoint)
这样,我可以一次为对象分配点的(x,y,z)分量,而无需单独设置它们.如果我必须分离每个组件,这可能很难看,尤其是如果每个类都包含多个这些复合对象时.我将这些值组合到一个编码字段中,只是我有时需要分别查询每个值.
This way I could assign an object with the (x, y, z) components of the point at once without setting them individually. If I had to separate each component, this could get ugly, especially if each class has several of these composite objects. I would combine the values into one encoded field except that I need to query each value separately at times.
我能够找到使用单一列创建自定义类型的方法文档中的.但是没有迹象表明我可以将单个类型映射到多个列.
I was able to find out how to make custom types using a single column in the documentation. But there's no indication that I can map a single type to multiple columns.
我想我可以通过使用单独的表来完成此操作,并且每一列都是外键,但是对于我而言,我认为为每个点到单独的表建立一对一的映射是没有道理的,但这仍然无法一次设置所有相关值.
I suppose I could accomplish this by using a separate table, and each column would be a foreign key, but in my case I don't think it makes sense to have a one to one mapping for each point to a separate table, and this still does not give the ability to set the related values all at once.
推荐答案
以下是基于 查看全文