Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread Zsolt Ero
I'm starting to understand it! Just a few very quick questions: 1. Is it a good strategy to always use onupdate='CASCADE', ondelete='CASCADE' for all foreign keys if the db supports it (in my case Postgres 9.5 + psycopg2)? 2. If I'd like to use it on an already populated db, can I "alter table"

Re: [sqlalchemy] questions regarding entity default load strategies

2017-02-15 Thread Gerald Thibault
> here are also "wildcard" keys but I don't think those cover exactly the use case you're trying to do, which is basically "Order.items everywhere in the query" If I uncomment the two commented out lines in my test, I can do session.query(User) and it is able to properly handle the join

Re: [sqlalchemy] questions regarding entity default load strategies

2017-02-15 Thread mike bayer
On 02/15/2017 04:39 PM, Gerald Thibault wrote: I have 3 classes, like so: | class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) class Order(Base): __tablename__ = 'orders' id = Column(Integer, primary_key=True) user_id = Column(Integer,

[sqlalchemy] Re: questions regarding entity default load strategies

2017-02-15 Thread Gerald Thibault
Here is a minimal script which shows what I'm trying to do and where things are going wrong. -- 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

[sqlalchemy] questions regarding entity default load strategies

2017-02-15 Thread Gerald Thibault
I have 3 classes, like so: class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) class Order(Base): __tablename__ = 'orders' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey(User.id)) user = relationship(User,

Re: [sqlalchemy] Using `INSERT...ON CONFLICT` with ORM

2017-02-15 Thread Mike Bayer
Well first off the ORM on the persistence side only identifies objects by primary key. Is "bar" the primary key here ? That would be one requirement. The semantics of INSERT on conflict most closely match those of Session.merge(). If you're dealing with primary key, merge will do this

[sqlalchemy] Using `INSERT...ON CONFLICT` with ORM

2017-02-15 Thread Calvin Young
I use the SQLAlchemy ORM in my application, and I know I can use something the following to perform an `INSERT...ON CONFLICT` statement: from sqlalchemy.dialects.postgresql import insert class Foo(Base): ... bar = Column(Integer) foo = Foo(bar=1) insert_stmt =

Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread mike bayer
On 02/15/2017 09:45 AM, Zsolt Ero wrote: 4. An interesting thing is that SQLAlchemy does 3 select calls in the delete case, even if 1 would be enough. Can be seen in the logs. the ORM only deletes rows one at a time based on primary key match. So if you have a relationship() that is

Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread Zsolt Ero
Thank you for the explanation! It is indeed deeper than I first thought, but I understand it now. Finally, consider question 4. (multiple select on delete) to be a bug report, even if harmless. On 15 February 2017 at 17:07, mike bayer wrote: > a "delete" cascade can

Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread mike bayer
a "delete" cascade can be done on the client side because it involves a simple ordering of steps: 1. DELETE the rows that depend on the target row 2. DELETE the target row. an "update" cascade, OTOH, can't work this way. Because the dependent row remains existing and needs to have a

Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread Zsolt Ero
Thanks a lot! I would be still interested in your answer for 3. and 4. Especially, what is the difference between update and delete from's behaviour here? Why does SQLAlchemy know how to "cascade" a delete just on the client side, while for update it needs server side CASCADE support? On 15

Re: [sqlalchemy] Unable to use the 'mock' strategy: AttributeError: 'MockConnection' object has no attribute 'run_callable'

2017-02-15 Thread Manuel
mike bayer writes: > On 02/14/2017 11:02 AM, Manuel wrote: >> Thanks, I'm still in the early stages of this project and any comments >> are highly appreciated. What I'm trying to actually accomplish is to >> build some complex queries to be executed against an Odoo [1]

Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread mike bayer
onupdate=CASCADE is an option of ForeignKey, not Column: Table( 'collections_images', Base.metadata, Column('collection_id', ForeignKey('collections.id', onupdate='CASCADE'), primary_key=True, ), Column('image_id', ForeignKey('images.id'), primary_key=True)) On

Re: [sqlalchemy] Changing primary key's value in a multi-to-multi relation

2017-02-15 Thread Zsolt Ero
Thanks Mike for looking into this. I've created a minimal program which reproduces my error. As a context, this is a init script for a Pyramid app, but is run from command line, not in a request loop. The tables are dropped and recreated at start. Inklesspen helped me figure out the transaction