On Fri, Jul 14, 2017 at 1:24 AM, David Laredo Razo
<davidlare...@gmail.com> 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
>> <davidl...@gmail.com> 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 sqlalchemy+...@googlegroups.com.
>> > To post to this group, send email to sqlal...@googlegroups.com.
>> > 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to