【发布时间】:2016-04-06 22:45:01
【问题描述】:
问题的标题可能令人困惑,但我相信我已经在下面描述了这个问题。
我有各种使用 SQLAlchemy 构建的声明性“测试类型”模型。每个“测试类型”模型都与位于“位置”上的一件“设备”相关联。因此,每个测试类型都有一个“location.name”参数,该参数位于远离实际测试模型的不同数量的关系中。我需要这个“location.name”值与每个“测试”对象相关联。
我现在设置它的方式在查询测试时会发出大量 SQL。我想这是因为关系级联。我将所有测试显示在一个表中,并且我希望能够根据测试关联的位置以某种方式过滤测试。
这个模型配置对我来说是新的,我不知道如何使用原始 SQL 与之交互,我只知道如何使用模型。
我想知道获取特定测试实例的位置名称参数的最佳方法是什么。请注意,每个测试的get_json() 函数都会返回location.name,尽管我希望有一种更好的方法可以在过滤所有测试类型时发出更少的SQL并且更简单。
希望下面的模型定义可以清楚地说明这一点
class Location(Model):
__tablename__ = 'locations'
id = Column(Integer, Sequence('location_id_seq'), primary_key=True)
name = Column(String(50), unique=True)
...
class EquipmentFoo(Model):
__tablename__ = 'equipmentfoos'
id = Column(Integer, Sequence('equipmentfoo_id_seq'), primary_key=True)
location_id = Column(Integer, ForeignKey('locations.id'))
location = relationship('Location', backref='equipmentfoos')
footests = relationship('FooTest', backref='equipmentfoo')
...
class EquipmentBar(Model):
__tablename__ = 'equipmentbars'
id = Column(Integer, Sequence('equipmentbar_id_seq'), primary_key=True)
equipmentfoo_id = Column(Integer, ForeignKey('equipmentfoos.id'))
equipmentfoo = relationship('EquipmentFoo', backref='equipmentbars')
bartests = relationship('BarTest', backref='equipmentbar')
...
class EquipmentZab(Model):
__tablename__ = 'equipmentzabs'
id = Column(Integer, Sequence('equipmentzab_id_seq'), primary_key=True)
equipmentbar_id = Column(Integer, ForeignKey('equipmentbars.id'))
equipmentbar = relationship('EquipmentBar', backref='equipmentzabs')
zabtests = relationship('ZabTest', backref='equipmentzab')
...
这里有设备模型以及它们与位置的关系。还包括它们与下面各自测试的关系。
class HasId(object):
@declared_attr
def id(cls):
return Column('id', Integer, Sequence('test_id_seq'), primary_key=True)
@declared_attr
def status(cls):
return Column('status', String(50))
...
class TestParent(HasId, Model):
__tablename__ = 'tests'
discriminator = Column(String(50))
__mapper_args__ = {'polymorphic_on': discriminator}
...
def parent_json(self):
return {'id': self.id,
'status': self.status,
...
}
class FooTest(TestParent):
__tablename__ = 'footests'
__mapper_args__ = {'polymorphic_identity': 'footests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True) cascade='save-update, merge')
pressure_start = Column(Float)
...
def get_json():
my_json = {'location': self.equipmentfoo.location.name,
'pressure_start': self.pressure_start,
...
}
parent_json = super(FooTest, self).parent_json()
my_json.update(parent_json)
return my_json
class BarTest(TestParent):
__tablename__ = 'bartests'
__mapper_args__ = {'polymorphic_identity': 'bartests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True) cascade='save-update, merge')
hatch_value = Column(Boolean)
...
def get_json():
my_json = {'location': self.equipmentbar.equipmentfoo.location.name,
'hatch_value': self.hatch_value,
...
}
parent_json = super(BarTest, self).parent_json()
my_json.update(parent_json)
return my_json
class ZabTest(TestParent):
__tablename__ = 'zabtests'
__mapper_args__ = {'polymorphic_identity': 'zabtests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True) cascade='save-update, merge')
safety_check = Column(Boolean)
...
def get_json():
my_json = {'location': self.equipmentzab.equipmentbar.equipmentfoo.location.name,
'safety_check': self.hatch_value,
...
}
parent_json = super(ZabTest, self).parent_json()
my_json.update(parent_json)
return my_json
【问题讨论】:
标签: python sqlalchemy