Sorry I made a mistake, when printing whats inside the session I do it this 
way

for new_object in session.new:
   print(new_object, mapper.identity_key_from_instance(new_object))

On Wednesday, July 12, 2017 at 11:31:06 PM UTC-5, David Laredo Razo wrote:
>
> Hello, I am using SQLAlchemy version 1.2.0b1
>
> I created some mapped objects using the declarative style in SQLAlchemy. I 
> have a mapping called ThermafuserReading which has a composed primary key 
> made up of the Time_stamp column which is DateTime and ThermafuserId column 
> which is an Integer and also acts as a Foreign Key to another table called 
> Thermafuser. This is the definition of the class
>
> class ThermafuserReading(Base):
> """Class to map to the Thermafuser Readings table in the HVAC DB"""
>
> __tablename__ = 'Thermafuser_Reading'
>
> _timestamp = Column('Time_stamp', DateTime, primary_key = True)
> _thermafuserId = Column('ThermafuserId', Integer, 
> ForeignKey("Thermafuser.ThermafuserId"), primary_key = True)
> _roomOccupied = Column('RoomOccupied', Boolean)
> _zoneTemperature = Column('ZoneTemperature', Float)
> _supplyAir = Column('SupplyAir', Float, nullable=True)
> _airflowFeedback = Column('AirflowFeedback', Float, nullable=True)
> _CO2Input = Column('CO2Input', Float, nullable=True)
> _maxAirflow = Column('MaxAirflow', Float, nullable=True)
> _minAirflow = Column('MinAirflow', Float, nullable=True)
> _unoccupiedHeatingSetpoint = Column('UnoccupiedHeatingSetpoint', Float, 
> nullable=True)
> _unoccupiedCoolingSetpoint = Column('UnoccupiedCoolingSetpoint', Float, 
> nullable=True)
> _occupiedCoolingSetpoint = Column('OccupiedCoolingSetpoint', Float, 
> nullable=True)
> _occupiedHeatingSetpoint = Column('OccupiedHeatingSetpoint', Float, 
> nullable=True)
> _terminalLoad = Column('TerminalLoad', Float, nullable=True)
>
> #Relationship between Thermafuser Reading and Thermafuser
> _thermafuser = relationship("Thermafuser", back_populates = 
> "_thermafuserReadings",  cascade = "all, delete-orphan", single_parent = True)
>
>
> I am creating a session in the following way
>
> sqlengine = 
> sqlalchemy.create_engine("mysql+mysqldb://user:password@localhost:3306/HVAC")
> Session = sessionmaker(bind=sqlengine)
> session = Session()
>
>
> At some point in my code I am creating a list called readings of 
> Thermafuser Readings and adding such list the session via 
> session.add_all(readings)
>
> This are some example elements printed from the list readings:
>
> for reading in readings:
>     print(reading, mapper.identity_key_from_instance(reading))
>
> <ThermafuserReading(thermafuserId = '1', timestamp = '2016-12-31 23:40:00')> 
> (<class '__main__.ThermafuserReading'>, (datetime.datetime(2016, 12, 31, 23, 
> 40), 1))
>
> <ThermafuserReading(thermafuserId = '1', timestamp = '2016-12-31 23:45:00')> 
> (<class '__main__.ThermafuserReading'>, (datetime.datetime(2016, 12, 31, 23, 
> 45), 1))
>
> <ThermafuserReading(thermafuserId = '1', timestamp = '2016-12-31 23:50:00')> 
> (<class '__main__.ThermafuserReading'>, (datetime.datetime(2016, 12, 31, 23, 
> 50), 1))
>
> <ThermafuserReading(thermafuserId = '1', timestamp = '2016-12-31 23:55:00')> 
> (<class '__main__.ThermafuserReading'>, (datetime.datetime(2016, 12, 31, 23, 
> 55), 1))
>
> <ThermafuserReading(thermafuserId = '1', timestamp = '2017-01-01 00:00:00')> 
> (<class '__main__.ThermafuserReading'>, (datetime.datetime(2017, 1, 1, 0, 0), 
> 1))
>
>
> 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.
>
> 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+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