Flask-Sqlalchemy saving children one-to-many issue












0














I am very confused and I am trying to add children to an existing parent object in the database. When trying to do that I get an error.



I am unsure about how to make sure my newly added children object gets saved?
The error and my attempts are below:



equityLinkedNote = db.session.query(EquityLinkedNote).filter(EquityLinkedNote.cmdb == cmdb_eln.cmdb)
underlier_list = underliers.to_dict(orient="records")
equityLinkedNote.Underlier =
[UnderlierDetails(
Ticker=underlier["Ticker"] if "Ticker" in underlier else None,
Weight=underlier["Weight"] if "Weight" in underlier else None,
ReturnFloor=underlier["ReturnFloor"] if "ReturnFloor" in underlier else None,
InitialFixing=underlier["InitialFixing"] if "InitialFixing" in underlier else None,
ReturnCap=underlier["ReturnCap"] if "ReturnCap" in underlier else None,
EffectiveDate=underlier["EffectiveDate"] if "EffectiveDate" in underlier else cmdb_eln.trade_date
) for underlier in underlier_list]
db.session.flush()
db.session.commit()


ERROR



File "....libsite-packagessqlalchemyextdeclarativebase.py", line 747, in _declarative_constructor
setattr(self, k, kwargs[k])
File "...site-packagessqlalchemyormattributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "...site-packagessqlalchemyormattributes.py", line 711, in set
state._modified_event(dict_, self, old)
AttributeError: 'NoneType' object has no attribute '_modified_event'


I have no idea why this is failing. any help will be appreciated!



Here are my models



class EquityLinkedNote(GenericSecMasterObject):
Underlier = db.relationship("UnderlierDetails", backref='note', cascade="save-update, merge, delete", lazy='dynamic',
collection_class=list)


Underlier



class UnderlierDetails(db.Model):
__tablename__ = "underlier_details"
id = db.Column(db.Integer, primary_key=True)
Ticker = db.Column(db.String(50), nullable=False)
InitialFixing = db.Column(db.Float, nullable=True)
ReturnCap = db.Column(db.Float, nullable=True)
ReturnFloor = db.Column(db.Float, nullable=True)
Weight = db.Column(db.Float, nullable=True)
EffectiveDate = db.Column(db.DateTime, nullable=False)
cmdb = db.Column(db.String(20), db.ForeignKey('sec_master_details.cmdb'), nullable=False)









share|improve this question
























  • Not really sure what you are trying to do here. You have not added anything to the session but you committing the session. Can you put more words into the post? It seems like you are querying some objects and want to update some fields
    – mad_
    Dec 27 '18 at 15:22












  • I added some more. Sorry its my first question on stack. thanks for your patience.
    – wills iyer
    Dec 27 '18 at 15:31










  • Still does not make sense. equityLinkedNote is a query object while Underliner is a dataframe. I am pretty sure query object is not related to a dataframe. You just need list of dicts which you can later pass to the session in order to update the objects.
    – mad_
    Dec 27 '18 at 15:34










  • i made it into a list of dicts using "underlier_list = underliers.to_dict(orient="records")"
    – wills iyer
    Dec 27 '18 at 15:35










  • Thanks for your help! @_mad
    – wills iyer
    Dec 27 '18 at 16:06
















0














I am very confused and I am trying to add children to an existing parent object in the database. When trying to do that I get an error.



I am unsure about how to make sure my newly added children object gets saved?
The error and my attempts are below:



equityLinkedNote = db.session.query(EquityLinkedNote).filter(EquityLinkedNote.cmdb == cmdb_eln.cmdb)
underlier_list = underliers.to_dict(orient="records")
equityLinkedNote.Underlier =
[UnderlierDetails(
Ticker=underlier["Ticker"] if "Ticker" in underlier else None,
Weight=underlier["Weight"] if "Weight" in underlier else None,
ReturnFloor=underlier["ReturnFloor"] if "ReturnFloor" in underlier else None,
InitialFixing=underlier["InitialFixing"] if "InitialFixing" in underlier else None,
ReturnCap=underlier["ReturnCap"] if "ReturnCap" in underlier else None,
EffectiveDate=underlier["EffectiveDate"] if "EffectiveDate" in underlier else cmdb_eln.trade_date
) for underlier in underlier_list]
db.session.flush()
db.session.commit()


ERROR



File "....libsite-packagessqlalchemyextdeclarativebase.py", line 747, in _declarative_constructor
setattr(self, k, kwargs[k])
File "...site-packagessqlalchemyormattributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "...site-packagessqlalchemyormattributes.py", line 711, in set
state._modified_event(dict_, self, old)
AttributeError: 'NoneType' object has no attribute '_modified_event'


I have no idea why this is failing. any help will be appreciated!



Here are my models



class EquityLinkedNote(GenericSecMasterObject):
Underlier = db.relationship("UnderlierDetails", backref='note', cascade="save-update, merge, delete", lazy='dynamic',
collection_class=list)


Underlier



class UnderlierDetails(db.Model):
__tablename__ = "underlier_details"
id = db.Column(db.Integer, primary_key=True)
Ticker = db.Column(db.String(50), nullable=False)
InitialFixing = db.Column(db.Float, nullable=True)
ReturnCap = db.Column(db.Float, nullable=True)
ReturnFloor = db.Column(db.Float, nullable=True)
Weight = db.Column(db.Float, nullable=True)
EffectiveDate = db.Column(db.DateTime, nullable=False)
cmdb = db.Column(db.String(20), db.ForeignKey('sec_master_details.cmdb'), nullable=False)









share|improve this question
























  • Not really sure what you are trying to do here. You have not added anything to the session but you committing the session. Can you put more words into the post? It seems like you are querying some objects and want to update some fields
    – mad_
    Dec 27 '18 at 15:22












  • I added some more. Sorry its my first question on stack. thanks for your patience.
    – wills iyer
    Dec 27 '18 at 15:31










  • Still does not make sense. equityLinkedNote is a query object while Underliner is a dataframe. I am pretty sure query object is not related to a dataframe. You just need list of dicts which you can later pass to the session in order to update the objects.
    – mad_
    Dec 27 '18 at 15:34










  • i made it into a list of dicts using "underlier_list = underliers.to_dict(orient="records")"
    – wills iyer
    Dec 27 '18 at 15:35










  • Thanks for your help! @_mad
    – wills iyer
    Dec 27 '18 at 16:06














0












0








0







I am very confused and I am trying to add children to an existing parent object in the database. When trying to do that I get an error.



I am unsure about how to make sure my newly added children object gets saved?
The error and my attempts are below:



equityLinkedNote = db.session.query(EquityLinkedNote).filter(EquityLinkedNote.cmdb == cmdb_eln.cmdb)
underlier_list = underliers.to_dict(orient="records")
equityLinkedNote.Underlier =
[UnderlierDetails(
Ticker=underlier["Ticker"] if "Ticker" in underlier else None,
Weight=underlier["Weight"] if "Weight" in underlier else None,
ReturnFloor=underlier["ReturnFloor"] if "ReturnFloor" in underlier else None,
InitialFixing=underlier["InitialFixing"] if "InitialFixing" in underlier else None,
ReturnCap=underlier["ReturnCap"] if "ReturnCap" in underlier else None,
EffectiveDate=underlier["EffectiveDate"] if "EffectiveDate" in underlier else cmdb_eln.trade_date
) for underlier in underlier_list]
db.session.flush()
db.session.commit()


ERROR



File "....libsite-packagessqlalchemyextdeclarativebase.py", line 747, in _declarative_constructor
setattr(self, k, kwargs[k])
File "...site-packagessqlalchemyormattributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "...site-packagessqlalchemyormattributes.py", line 711, in set
state._modified_event(dict_, self, old)
AttributeError: 'NoneType' object has no attribute '_modified_event'


I have no idea why this is failing. any help will be appreciated!



Here are my models



class EquityLinkedNote(GenericSecMasterObject):
Underlier = db.relationship("UnderlierDetails", backref='note', cascade="save-update, merge, delete", lazy='dynamic',
collection_class=list)


Underlier



class UnderlierDetails(db.Model):
__tablename__ = "underlier_details"
id = db.Column(db.Integer, primary_key=True)
Ticker = db.Column(db.String(50), nullable=False)
InitialFixing = db.Column(db.Float, nullable=True)
ReturnCap = db.Column(db.Float, nullable=True)
ReturnFloor = db.Column(db.Float, nullable=True)
Weight = db.Column(db.Float, nullable=True)
EffectiveDate = db.Column(db.DateTime, nullable=False)
cmdb = db.Column(db.String(20), db.ForeignKey('sec_master_details.cmdb'), nullable=False)









share|improve this question















I am very confused and I am trying to add children to an existing parent object in the database. When trying to do that I get an error.



I am unsure about how to make sure my newly added children object gets saved?
The error and my attempts are below:



equityLinkedNote = db.session.query(EquityLinkedNote).filter(EquityLinkedNote.cmdb == cmdb_eln.cmdb)
underlier_list = underliers.to_dict(orient="records")
equityLinkedNote.Underlier =
[UnderlierDetails(
Ticker=underlier["Ticker"] if "Ticker" in underlier else None,
Weight=underlier["Weight"] if "Weight" in underlier else None,
ReturnFloor=underlier["ReturnFloor"] if "ReturnFloor" in underlier else None,
InitialFixing=underlier["InitialFixing"] if "InitialFixing" in underlier else None,
ReturnCap=underlier["ReturnCap"] if "ReturnCap" in underlier else None,
EffectiveDate=underlier["EffectiveDate"] if "EffectiveDate" in underlier else cmdb_eln.trade_date
) for underlier in underlier_list]
db.session.flush()
db.session.commit()


ERROR



File "....libsite-packagessqlalchemyextdeclarativebase.py", line 747, in _declarative_constructor
setattr(self, k, kwargs[k])
File "...site-packagessqlalchemyormattributes.py", line 229, in __set__
instance_dict(instance), value, None)
File "...site-packagessqlalchemyormattributes.py", line 711, in set
state._modified_event(dict_, self, old)
AttributeError: 'NoneType' object has no attribute '_modified_event'


I have no idea why this is failing. any help will be appreciated!



Here are my models



class EquityLinkedNote(GenericSecMasterObject):
Underlier = db.relationship("UnderlierDetails", backref='note', cascade="save-update, merge, delete", lazy='dynamic',
collection_class=list)


Underlier



class UnderlierDetails(db.Model):
__tablename__ = "underlier_details"
id = db.Column(db.Integer, primary_key=True)
Ticker = db.Column(db.String(50), nullable=False)
InitialFixing = db.Column(db.Float, nullable=True)
ReturnCap = db.Column(db.Float, nullable=True)
ReturnFloor = db.Column(db.Float, nullable=True)
Weight = db.Column(db.Float, nullable=True)
EffectiveDate = db.Column(db.DateTime, nullable=False)
cmdb = db.Column(db.String(20), db.ForeignKey('sec_master_details.cmdb'), nullable=False)






python flask flask-sqlalchemy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 16:21

























asked Dec 27 '18 at 15:14









wills iyer

12




12












  • Not really sure what you are trying to do here. You have not added anything to the session but you committing the session. Can you put more words into the post? It seems like you are querying some objects and want to update some fields
    – mad_
    Dec 27 '18 at 15:22












  • I added some more. Sorry its my first question on stack. thanks for your patience.
    – wills iyer
    Dec 27 '18 at 15:31










  • Still does not make sense. equityLinkedNote is a query object while Underliner is a dataframe. I am pretty sure query object is not related to a dataframe. You just need list of dicts which you can later pass to the session in order to update the objects.
    – mad_
    Dec 27 '18 at 15:34










  • i made it into a list of dicts using "underlier_list = underliers.to_dict(orient="records")"
    – wills iyer
    Dec 27 '18 at 15:35










  • Thanks for your help! @_mad
    – wills iyer
    Dec 27 '18 at 16:06


















  • Not really sure what you are trying to do here. You have not added anything to the session but you committing the session. Can you put more words into the post? It seems like you are querying some objects and want to update some fields
    – mad_
    Dec 27 '18 at 15:22












  • I added some more. Sorry its my first question on stack. thanks for your patience.
    – wills iyer
    Dec 27 '18 at 15:31










  • Still does not make sense. equityLinkedNote is a query object while Underliner is a dataframe. I am pretty sure query object is not related to a dataframe. You just need list of dicts which you can later pass to the session in order to update the objects.
    – mad_
    Dec 27 '18 at 15:34










  • i made it into a list of dicts using "underlier_list = underliers.to_dict(orient="records")"
    – wills iyer
    Dec 27 '18 at 15:35










  • Thanks for your help! @_mad
    – wills iyer
    Dec 27 '18 at 16:06
















Not really sure what you are trying to do here. You have not added anything to the session but you committing the session. Can you put more words into the post? It seems like you are querying some objects and want to update some fields
– mad_
Dec 27 '18 at 15:22






Not really sure what you are trying to do here. You have not added anything to the session but you committing the session. Can you put more words into the post? It seems like you are querying some objects and want to update some fields
– mad_
Dec 27 '18 at 15:22














I added some more. Sorry its my first question on stack. thanks for your patience.
– wills iyer
Dec 27 '18 at 15:31




I added some more. Sorry its my first question on stack. thanks for your patience.
– wills iyer
Dec 27 '18 at 15:31












Still does not make sense. equityLinkedNote is a query object while Underliner is a dataframe. I am pretty sure query object is not related to a dataframe. You just need list of dicts which you can later pass to the session in order to update the objects.
– mad_
Dec 27 '18 at 15:34




Still does not make sense. equityLinkedNote is a query object while Underliner is a dataframe. I am pretty sure query object is not related to a dataframe. You just need list of dicts which you can later pass to the session in order to update the objects.
– mad_
Dec 27 '18 at 15:34












i made it into a list of dicts using "underlier_list = underliers.to_dict(orient="records")"
– wills iyer
Dec 27 '18 at 15:35




i made it into a list of dicts using "underlier_list = underliers.to_dict(orient="records")"
– wills iyer
Dec 27 '18 at 15:35












Thanks for your help! @_mad
– wills iyer
Dec 27 '18 at 16:06




Thanks for your help! @_mad
– wills iyer
Dec 27 '18 at 16:06












1 Answer
1






active

oldest

votes


















0














I realized my error, for anyone who has similair issue, refrain from using the following in a db.Model, removed it and the error was resolved...



def __getattr__(self, item):
return None





share|improve this answer





















  • yeah, that's the kind of thing that can bite you. Why was that in there?
    – Paul Becotte
    Dec 27 '18 at 16:12










  • i was defining a property using it
    – wills iyer
    Dec 27 '18 at 16:15










  • probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
    – Paul Becotte
    Dec 27 '18 at 16:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947185%2fflask-sqlalchemy-saving-children-one-to-many-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I realized my error, for anyone who has similair issue, refrain from using the following in a db.Model, removed it and the error was resolved...



def __getattr__(self, item):
return None





share|improve this answer





















  • yeah, that's the kind of thing that can bite you. Why was that in there?
    – Paul Becotte
    Dec 27 '18 at 16:12










  • i was defining a property using it
    – wills iyer
    Dec 27 '18 at 16:15










  • probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
    – Paul Becotte
    Dec 27 '18 at 16:24
















0














I realized my error, for anyone who has similair issue, refrain from using the following in a db.Model, removed it and the error was resolved...



def __getattr__(self, item):
return None





share|improve this answer





















  • yeah, that's the kind of thing that can bite you. Why was that in there?
    – Paul Becotte
    Dec 27 '18 at 16:12










  • i was defining a property using it
    – wills iyer
    Dec 27 '18 at 16:15










  • probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
    – Paul Becotte
    Dec 27 '18 at 16:24














0












0








0






I realized my error, for anyone who has similair issue, refrain from using the following in a db.Model, removed it and the error was resolved...



def __getattr__(self, item):
return None





share|improve this answer












I realized my error, for anyone who has similair issue, refrain from using the following in a db.Model, removed it and the error was resolved...



def __getattr__(self, item):
return None






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 27 '18 at 16:05









wills iyer

12




12












  • yeah, that's the kind of thing that can bite you. Why was that in there?
    – Paul Becotte
    Dec 27 '18 at 16:12










  • i was defining a property using it
    – wills iyer
    Dec 27 '18 at 16:15










  • probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
    – Paul Becotte
    Dec 27 '18 at 16:24


















  • yeah, that's the kind of thing that can bite you. Why was that in there?
    – Paul Becotte
    Dec 27 '18 at 16:12










  • i was defining a property using it
    – wills iyer
    Dec 27 '18 at 16:15










  • probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
    – Paul Becotte
    Dec 27 '18 at 16:24
















yeah, that's the kind of thing that can bite you. Why was that in there?
– Paul Becotte
Dec 27 '18 at 16:12




yeah, that's the kind of thing that can bite you. Why was that in there?
– Paul Becotte
Dec 27 '18 at 16:12












i was defining a property using it
– wills iyer
Dec 27 '18 at 16:15




i was defining a property using it
– wills iyer
Dec 27 '18 at 16:15












probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
– Paul Becotte
Dec 27 '18 at 16:24




probably want to stick to @property for that- too easy to break stuff if you override the magic methods.
– Paul Becotte
Dec 27 '18 at 16:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947185%2fflask-sqlalchemy-saving-children-one-to-many-issue%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas