1
我是使用SQLAlchemy創建ORM的新手,我僅用於使用原始SQL。我有數據庫表,Label
,Position
,並且DataSet
類似以下內容:如何在SQLAlchemy中合併兩個模型類
以及相應的Python類如下:
class Label(Base):
__tablename__ = 'Label'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False, unique=true)
class Position(Base):
__tablename__ = 'Position'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False, unique=true)
class DataSet(Base):
__tablename__ = 'DataSet'
id = Column(Integer, primary_key=True)
label_id = Column(Integer, ForeignKey('Label.id'))
position_id = Column(Integer, ForeignKey('Position.id'))
timestamp = Column(Integer, nullable=False)
但在我役,我不揭露那些label_id
和position_id
。所以我做了一個新的課Data
作爲字符串持有0和position
。
# Not a full class to only show my concept
class Data:
# data dictionary will have data
def __init__(self, **kwargs):
# So it doesn't have ids. Label and Position as string
keys = {'label', 'position', 'timestamp'}
self.data = {k: kwargs[k] for k in keys}
# An example of inserting data.
# skipped detail and error handling to clarify
def insert(self):
session = Session()
# get id of label and position
# remember that it returns a tuple, not a single value
self.data['label_id'] = session.query(Label.id).\
filter(Label.name == self.data['label']).one_or_none()
self.data['position_id'] = session.query(Position.id).\
filter(Position.name == self.data['position']).one_or_none()
# add new dataset
self.data.pop('label')
self.data.pop('position')
new_data = DataSet(**self.data)
session.add(new_data)
session.commit()
但它看起來有點難看,我認爲應該有一個更簡單的方法來做到這一點。有什麼辦法可以使用SQLAlchemy API來組合這些表類嗎?
我剛剛閱讀了關於關係,但不知道關聯代理。這將節省我的時間。非常感謝你! –