0

我在 sqlalchemy 中遇到了以下问题。我在sqlite中制作了三个不同的表,第一个没有关系,第二个与第一个有关系,第三个与第二个有关系。因此,当我想在第一个和第二个表中插入东西时,一切正常。当我想在第三个表中插入数据时,当我要像教程中描述的那样进行操作时会遇到麻烦。这是我的三个表的代码:

第一张表:

# Save_Data_Type.py

from sqlalchemy import ForeignKey
from sqlalchemy import Column, Float, Integer, String
from base import Base
from sqlalchemy.orm import relationship, backref

########################################################################
class Save_Data_Type(Base):

__tablename__ = "save_datas_type"

save_datas_type_id = Column(Integer, primary_key=True)
type_memory = Column(String)
comment = Column(String) 
dummy1 = Column(String)
dummy2 = Column(String) 
#----------------------------------------------------------------------
def __init__(self, type_memory, comment, dummy1, dummy2):
    """"""
    self.type_memory = type_memory
    self.comment = comment
    self.dummy1 = dummy1
    self.dummy2 = dummy2

第二张表

# Save_Data.py
from sqlalchemy import ForeignKey
from sqlalchemy import Column, Float, Integer, String
from base import Base
from sqlalchemy.orm import relationship, backref


########################################################################
class Save_Data(Base):
""""""
__tablename__ = "save_datas"

save_datas_id = Column(Integer, primary_key=True)

save_datas_type_id  = Column(Integer,ForeignKey("save_datas_type.save_datas_type_id"))
save_datas_type = relationship("Save_Data_Type", backref=backref("save_datas", order_by=save_datas_id))

value = Column(Float)

comment = Column(String)

dummy1 = Column(String)


#----------------------------------------------------------------------
def __init__(self, value, comment, dummy1):
    """"""
    self.value = value
    self.comment = comment
    self.dummy1 = dummy1

第三表

# Station.py
from sqlalchemy import ForeignKey
from sqlalchemy import Column, Integer, Boolean, String
from base import Base
from sqlalchemy.orm import relationship, backref


########################################################################
class Station(Base):
""""""
__tablename__ = "stations"

stations_id = Column(Integer, primary_key=True)
name = Column(String)  
password = Column(String) 

save_datas_id = Column(Integer,ForeignKey("save_datas.save_datas_id"))
save_datas = relationship("Save_Data", backref=backref("stations", order_by=stations_id))


dummy1 = Column(String)
dummy2 = Column(String)
dummy3 = Column(String)


#----------------------------------------------------------------------
def __init__(self, name, password, dummy1, dummy2, dummy3):
    """"""
    self.name = name     
    self.password = password
    self.dummy1 = dummy1
    self.dummy2 = dummy2
    self.dummy3 = dummy3

基础.py

# base.py

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

因此,如果我要插入这样的数据:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

import base

from Save_Data_Type import Save_Data_Type
from Save_Data import Save_Data
from Station import Station

engine = create_engine('sqlite:///Database.db', echo=True)

base.Base.metadata.create_all(engine, checkfirst=True)

# create a Session
Session = sessionmaker(bind=engine)
session = Session()


jack = Save_Data_Type("Ring Memory",'In seconds',None,None)
jack.save_datas = [Save_Data(1980,'Sometimes more, sometimes less',None)]
jack.save_datas.stations = [Station('name1','123456',None,None,None)]

session.add(jack)

session.commit()

数据库的第三个表中没有写入任何内容。建立这种关系的通常方法是什么?

提前致谢,

约翰内斯

4

1 回答 1

0

除了您发布的代码中的一些拼写错误(参见 zzzeek 的评论),看起来问题将通过替换代码来解决:

jack.save_datas.stations = [Station('name1','123456',None,None,None)]

与下面的一个:

jack.save_datas[0].stations = [Station('name1','123456',None,None,None)]

原因如下:在您的情况下,您将stations属性分配/创建给类​​成员。这不仅不能做正确的工作,它实际上可能会损害您的 ORM 模型。在第二种情况下,您正确地Station实例分配给第一个save_datas.
更清晰的代码如下所示:

jack = Save_Data_Type("Ring Memory",'In seconds',None,None)

save_data1 = Save_Data(1980,'Sometimes more, sometimes less',None)
jack.save_datas.append(save_data1)
# or: jack.save_datas = [save_data1]
# or (my favourite): save_data1.Save_Data_Type = jack

save_data1.stations = [Station('name1','123456',None,None,None)]
# or... similar to the relationship above
于 2013-05-09T10:49:44.523 回答