Хорошо, я понял это. Не уверен, что этот вопрос привлечет внимание других людей, но я уверен, что это может быть полезно для кого-то. Вот мое решение (SQLAlchemy + Marshmallow):
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, ForeignKey, String, Float, Boolean
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
from marshmallow import fields
import json
from sqlalchemy import create_engine
engine = create_engine('sqlite:///test.db', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
# parent
class BessAsset(Base):
__tablename__ = 'bess_asset'
designation = Column(String, primary_key=True)
status = Column(Boolean)
vNom = Column(Float)
eNom = Column(Float)
maxSoc = Column(Float)
minSoc = Column(Float)
maxCCh = Column(Float)
maxCDch = Column(Float)
minPCh = Column(Float)
minPDch = Column(Float)
chEff = Column(Float)
dischEff = Column(Float)
eolCriterion = Column(Float)
lifetime = Column(Integer)
invPNom = Column(Float)
invVNom = Column(Float)
# one to one mapping
testData = relationship('TestData', backref='bessAsset', lazy='select', uselist=False)
# add other testData elements relationships here
# one to one relationship
# one bessAsset will have one testdata
class TestData(Base):
__tablename__ = 'test_data'
test_data_id = Column(Integer, primary_key=True)
designation = Column(String, ForeignKey('bess_asset.designation'))
vNomD = relationship('Vnomd', backref='testData', lazy='select')
vNomC = relationship('Vnomc', backref='testData', lazy='select')
dLim = relationship('Dlim', backref='testData', lazy='select')
cLim = relationship('Clim', backref='testData', lazy='select')
effD = relationship('Effd', backref='testData', lazy='select')
effC = relationship('Effc', backref='testData', lazy='select')
roundEff = relationship('RoundEff', backref='testData', lazy='select')
class RoundEff(Base):
__tablename__ = 'round_eff'
roundeff_id = Column(Integer, primary_key=True)
cRate = Column(Float)
roundEffAvg = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class Effc(Base):
__tablename__ = 'eff_c'
effc_id = Column(Integer, primary_key=True)
cRate = Column(Float)
effChAvg = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class Effd(Base):
__tablename__ = 'eff_d'
effd_id = Column(Integer, primary_key=True)
cRate = Column(Float)
effDchAvg = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class Clim(Base):
__tablename__ = 'clim'
clim_id = Column(Integer, primary_key=True)
cRate = Column(Float)
eRemain = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class Dlim(Base):
__tablename__ = 'dlim'
dlim_id = Column(Integer, primary_key=True)
cRate = Column(Float)
eRemain = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class Vnomd(Base):
__tablename__ = 'vnomd'
vnomd_id = Column(Integer, primary_key=True)
cRate = Column(Float)
vAvg = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class Vnomc(Base):
__tablename__ = 'vnomc'
vnomc_id = Column(Integer, primary_key=True)
cRate = Column(Float)
vAvg = Column(Float)
trial = Column(Integer)
test_data_id = Column(Integer, ForeignKey('test_data.test_data_id'))
class VnomDSchema(SQLAlchemyAutoSchema):
class Meta:
model = Vnomd
exclude = ['vnomd_id']
class VnomCSchema(SQLAlchemyAutoSchema):
class Meta:
model = Vnomc
exclude = ['vnomc_id']
class RoundEffSchema(SQLAlchemyAutoSchema):
class Meta:
model = RoundEff
exclude = ['roundeff_id']
class DlimSchema(SQLAlchemyAutoSchema):
class Meta:
model = Dlim
exclude = ['dlim_id']
class ClimSchema(SQLAlchemyAutoSchema):
class Meta:
model = Clim
exclude = ['clim_id']
# include test_data nested objects here
# will not be a list
class TestDataSchema(SQLAlchemyAutoSchema):
vNomD = fields.Nested(VnomDSchema, many=True)
vNomC = fields.Nested(VnomCSchema, many=True)
dLim = fields.Nested(DlimSchema, many=True)
cLim = fields.Nested(ClimSchema, many=True)
roundEff = fields.Nested(RoundEffSchema, many=True)
class Meta:
model = TestData
exclude = ['test_data_id', 'bessAsset']
include_relationships = True
class BessAssetSchema(SQLAlchemyAutoSchema):
testData = fields.Nested(TestDataSchema)
class Meta:
model = BessAsset
ordered = True # returns a ordered dict
# include_fk = True
# exclude = ['id', 'director_id']
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
vnomc = Vnomc(cRate=0.2, vAvg=0.34, trial=3)
vnomd = Vnomd(cRate=0.2, vAvg=0.34, trial=3)
roundEff = RoundEff(cRate=0.3, roundEffAvg=2.1, trial=2)
dlim = Dlim(cRate=2.5, eRemain=3.4, trial=100)
clim = Clim(cRate=2.5, eRemain=3.4, trial=200)
test_data = TestData(vNomC=[vnomc],
vNomD=[vnomd],
roundEff=[roundEff],
dLim=[dlim],
cLim=[clim])
bess_asset = BessAsset(designation='Teste 1',
status=True,
vNom=0.2,
eNom=0.3,
minPCh=2.1,
maxCCh=20.3,
minSoc=20.1,
maxSoc=1.3,
testData=test_data)
session.add(bess_asset)
session.commit()
bess_assets = session.query(BessAsset).all()
my_schema = BessAssetSchema()
my_data = my_schema.dump(bess_assets, many=True)
print(my_data)