Re: [sqlalchemy] session.add with insert-or-update

2020-03-09 Thread Keith Edmunds
Thanks Jonathan. This is a very low traffic application, so not a problem but I appreciate you mentioning it. -- 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

Re: [sqlalchemy] session.add with insert-or-update

2020-03-09 Thread Jonathan Vanasco
FWIW: If your application is high-traffic/high-concurrency, depending on how your transactions are scoped within the code you may want to do the getcreate or create step that calls `.flush` within an exception block or savepoint, to catch duplicate inserts. I've only had to do this on 2 (out

Re: [sqlalchemy] session.add with insert-or-update

2020-03-07 Thread 'Michael Mulqueen' via sqlalchemy
Hi Keith, Small world! You have at least 3 options depending on your requirements: 1. Handle it in your own application logic (e.g. make a get_or_create method) - I tend to prefer this, business rules for create vs. update often creeps in. 2. Use session.merge

[sqlalchemy] session.add with insert-or-update

2020-03-07 Thread Keith Edmunds
I'm new to SQLAlchemy. Sorry if it shows. I'm using a MySQL backend. I've set up a declarative_base, defined a table class, set up a session. I defined a record as a dictionary and added it successfully to the db with: incident = Incidents(**record) session.add(incident) session.commit() The

Re: [sqlalchemy] session.add() neglecting some of my objects to be added

2017-07-18 Thread David Laredo Razo
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

Re: [sqlalchemy] session.add() neglecting some of my objects to be added

2017-07-14 Thread Mike Bayer
On Fri, Jul 14, 2017 at 1:24 AM, David Laredo Razo 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 =

Re: [sqlalchemy] session.add() neglecting some of my objects to be added

2017-07-13 Thread David Laredo Razo
i did as you instructed me but the error persists. This is the example code im talking about Session = sessionmaker() session = Session() mapper = inspect(ThermafuserReading) readings = list() header = ["hex(id(object))", "is transient", "is pending", "is persistent", "is detached", "is

Re: [sqlalchemy] session.add() neglecting some of my objects to be added

2017-07-13 Thread Mike Bayer
On Thu, Jul 13, 2017 at 12:31 AM, David Laredo Razo 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.

[sqlalchemy] session.add() neglecting some of my objects to be added

2017-07-12 Thread David Laredo Razo
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

[sqlalchemy] session.add order

2015-02-15 Thread Victor Poluksht
I've found that sometimes sqlalchemy inserts objects during session.commit() not in the order they have been added using session.add() I've put my code example and the output to the github gist. https://gist.github.com/vpol/8da4a512308ae351eaf6 My question is similar to this one:

Re: [sqlalchemy] session.add order

2015-02-15 Thread Michael Bayer
the order in which you put things into session.add() is significant only within the scope of a certain mapped class, that is, the order that you put a bunch of Foo() objects into session.add() will be maintained, however a series of Bar() objects are handled separately. Between different object

[sqlalchemy] Session.add doesn't work on Python 3.3.0?

2013-04-16 Thread Tim Cooijmans
I have a strange error using Python 3.3.0 and SQLAlchemy 0.8.0: from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKeyfrom sqlalchemy.orm import sessionmaker, relationship, backreffrom sqlalchemy.ext.declarative import declarative_base engine =

Re: [sqlalchemy] Session.add doesn't work on Python 3.3.0?

2013-04-16 Thread Mauricio de Abreu Antunes
I think you need to commit your session data. Em terça-feira, 16 de abril de 2013, Tim Cooijmans escreveu: I have a strange error using Python 3.3.0 and SQLAlchemy 0.8.0: from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKeyfrom sqlalchemy.orm

Re: [sqlalchemy] Session.add doesn't work on Python 3.3.0?

2013-04-16 Thread Michael Bayer
cant reproduce, running with Python 3.3.0 here against 0.8.0 I get the INSERT: PRAGMA table_info(products) 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine () 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine CREATE TABLE products ( id INTEGER NOT NULL, name

Re: [sqlalchemy] Session.add doesn't work on Python 3.3.0?

2013-04-16 Thread Mauricio de Abreu Antunes
I tried your same code here I got True. Em terça-feira, 16 de abril de 2013, Michael Bayer escreveu: cant reproduce, running with Python 3.3.0 here against 0.8.0 I get the INSERT: PRAGMA table_info(products) 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine () 2013-04-16

Re: [sqlalchemy] Session.add performance

2010-12-16 Thread Michael Bayer
On Dec 16, 2010, at 12:39 AM, Julian Scheid wrote: In an application that is heavy on inserts and updates, cProfile output is dominated by Session.add in which about 45% of time is spent. Most of that time, in turn, is spent in cascade_iterator (43%). I can provide more detailed information

[sqlalchemy] Session.add performance

2010-12-15 Thread Julian Scheid
In an application that is heavy on inserts and updates, cProfile output is dominated by Session.add in which about 45% of time is spent. Most of that time, in turn, is spent in cascade_iterator (43%). I can provide more detailed information if needed. The application does aggressive caching of

[sqlalchemy] session.add() vs session.merge() and delete child

2010-01-30 Thread avdd
I'm using session.add() to refresh my objects while working on them, because I don't want to merge them with the persistent state. But it appears deletes aren't carrying across to child relations: $ cat listdelete.py; python listdelete.py import sqlalchemy as sql from sqlalchemy import orm

Re: [sqlalchemy] session.add() vs session.merge() and delete child

2010-01-30 Thread Michael Bayer
On Jan 30, 2010, at 9:07 AM, avdd wrote: I'm using session.add() to refresh my objects while working on them, because I don't want to merge them with the persistent state. But it appears deletes aren't carrying across to child relations: this example is too compliated for me to understand

Re: [sqlalchemy] session.add() vs session.merge() and delete child

2010-01-30 Thread Michael Bayer
On Jan 30, 2010, at 9:07 AM, avdd wrote: I'm using session.add() to refresh my objects while working on them, because I don't want to merge them with the persistent state. But it appears deletes aren't carrying across to child relations: in r6711 the save-update cascade operation will

[sqlalchemy] session.add() or session.update() ? (NEWBIE)

2009-03-27 Thread Marcin Krol
Hello everyone, I have tested that session.add(changed_sqla_object) in at least one context (when the object with id of changed_sqla_object already exists in the db) does issue UPDATE sql query and updates the object in the database. However, there's also session.update() method. Help on

[sqlalchemy] session.add

2008-12-11 Thread n00b
hello, i need to generate a unique number from a table based on the primary key. i used to lock the table (with write) and got what i needed. working with the ORM, though, it seems that session.add(object) functions just as well. at the time of the session.add(), the primary key assigned to the