Thanks Mike, indeed, that was the problem. I solved it using an "ad hoc" copy function. This is the solution that worked for me in case somebody else incurs in the same mistake I did
def copy_sqla_object(obj, omit_fk=True): """Given an SQLAlchemy object, creates a new object (FOR WHICH THE OBJECT MUST SUPPORT CREATION USING __init__() WITH NO PARAMETERS), and copies across all attributes, omitting PKs, FKs (by default), and relationship attributes.""" cls = type(obj) mapper = class_mapper(cls) newobj = cls() # not: cls.__new__(cls) pk_keys = set([c.key for c in mapper.primary_key]) rel_keys = set([c.key for c in mapper.relationships]) prohibited = pk_keys | rel_keys if omit_fk: fk_keys = set([c.key for c in mapper.columns if c.foreign_keys]) prohibited = prohibited | fk_keys for k in [p.key for p in mapper.iterate_properties if p.key not in prohibited]: try: value = getattr(obj, k) setattr(newobj, k, value) except AttributeError: pass return newobj On Friday, July 14, 2017 at 2:47:45 PM UTC-5, Mike Bayer wrote: > > On Fri, Jul 14, 2017 at 1:24 AM, David Laredo Razo > <[email protected] <javascript:>> wrote: > > > this code is the problem: > > > > > new_object = copy.copy(reading) > > copy() will copy the _sa_instance_state and prevent the session from > tracking the object correctly. > > Correct pattern should be: > > new_object = ThermafuserReading(None, componentId) > > Only when you call the constructor (e.g. ThermafuserReading.__init__) > do you get a new InstanceState object dedicated to that object. So > don't use copy(). > > There *are* ways to use copy() here instead but they are non-obvious > and not necessary for a simple case like this. > > > > > > new_object.timestamp = timestamp > > > > readings.append(new_object) > > > > #print(new_object, mapper.identity_key_from_instance(new_object)) > > #session.add(new_object) > > > > row_format = "{:>15}" * (len(header) + 1) > > > > print("Before adding to the session") > > print(row_format.format("", *header)) > > for reading in readings: > > insp = inspect(reading) > > row = [hex(id(reading)), insp.transient, insp.pending, insp.persistent, > > insp.detached, insp.deleted, reading in session] > > print(row_format.format("", *row)) > > > > session.add_all(readings) > > > > print("\n#Elements in the session") > > print(session) > > for element in session: > > print(element) > > > > print("\nAfter adding to the session") > > print(row_format.format("", *header)) > > for reading in readings: > > insp = inspect(reading) > > row = [hex(id(reading)), insp.transient, insp.pending, insp.persistent, > > insp.detached, insp.deleted, reading in session] > > print(row_format.format("", *row)) > > > > These are some results I obtained by comparing wheter the objects in my > list > > are in the session or not > > > > > > > > > > As you can observe, according to the results above the objects are > indeed > > inside the session but for some reason when I try to print whats > contained > > in the session by doing > > > > for element in session: > > print(element) > > > > I just get a None, what am I doing wrong? I dont see anything wrong in > my > > code, I hope you can help me clarify this. Thanks in advance. > > > > I will attach both my code and the tests data in case you want to try it > by > > yourself. > > > > > > > > > > > > On Thursday, July 13, 2017 at 8:27:04 AM UTC-5, Mike Bayer wrote: > >> > >> On Thu, Jul 13, 2017 at 12:31 AM, David Laredo Razo > >> <[email protected]> wrote: > >> > Hello, I am using SQLAlchemy version 1.2.0b1 > >> > > >> > > >> > > >> > So far so go, the problem arises when I add readings to the session > via > >> > session.add_all(readings). I only get the last element in my list > added, > >> > e.g. > >> > >> there's no reason at all that would happen, other than what's in > >> "readings" is not what you'd expect. > >> > >> try iterating through every element in "readings" after the add_all(), > >> and do "obj in session". > >> > >> If some of these objects were from a different session, then they may > >> be "detached" as you put them in in which case they'd go into > >> session.identity_map, not session.new. > >> > >> > >> > >> > >> > > >> > for new in session.new: > >> > print(new, mapper.identity_key_from_instance(new_object)) > >> > > >> > <ThermafuserReading(thermafuserId = '1', timestamp = '2017-01-01 > >> > 00:00:00')> > >> > (<class '__main__.ThermafuserReading'>, (datetime.datetime(2017, 1, > 1, > >> > 0, > >> > 0), 1)) > >> > > >> > > >> > Why is this behavior? I have a test code and the test data in case > its > >> > needed to reproduce this behavior > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >> > -- > >> > SQLAlchemy - > >> > The Python SQL Toolkit and Object Relational Mapper > >> > > >> > http://www.sqlalchemy.org/ > >> > > >> > To post example code, please provide an MCVE: Minimal, Complete, and > >> > Verifiable Example. See http://stackoverflow.com/help/mcve for a > full > >> > description. > >> > --- > >> > You received this message because you are subscribed to the Google > >> > Groups > >> > "sqlalchemy" group. > >> > To unsubscribe from this group and stop receiving emails from it, > send > >> > an > >> > email to [email protected]. > >> > To post to this group, send email to [email protected]. > >> > Visit this group at https://groups.google.com/group/sqlalchemy. > >> > For more options, visit https://groups.google.com/d/optout. > > > > -- > > SQLAlchemy - > > The Python SQL Toolkit and Object Relational Mapper > > > > http://www.sqlalchemy.org/ > > > > To post example code, please provide an MCVE: Minimal, Complete, and > > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > > description. > > --- > > You received this message because you are subscribed to the Google > Groups > > "sqlalchemy" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to [email protected] <javascript:>. > > To post to this group, send email to [email protected] > <javascript:>. > > Visit this group at https://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/d/optout. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
