Re: [sqlalchemy] Multi-table deletes with PostgreSQL

2016-09-16 Thread Mike Bayer



On 09/16/2016 12:57 PM, Alex Grönholm wrote:

I'm attempting to do a multi-table delete against PostgreSQL (psycopg2) with 
the following query:


session.query(ProductionItem).\
filter(Project.id == ProductionItem.project_id,

   Project.code.in_(projects),

   ProductionItem.external_id.is_(None)).\

delete(synchronize_session=False)


But it produces incorrect SQL. PostgreSQL requires the following syntax for 
this query:


DELETE FROM production_items USING projects WHERE 
production_items.project_id = project.id AND project.code IN (...) AND 
production_items.external_id IS NONE



DELETE USING is not supported right now.   I would suggest textual SQL 
or using a correlated subquery here.






Instead, I get this:


DELETE FROM production_items WHERE production_items.project_id = project.id 
AND project.code IN (...) AND production_items.external_id IS NONE


At which point PG complains:


sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) missing FROM-clause 
entry for table "projects"


From initial research this seems like a missing feature. Would it be
possible to add this to the postgresql dialect somehow? I might be
willing to contribute the code in that case.

--
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.


--
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] Multi-table deletes with PostgreSQL

2016-09-16 Thread Alex Grönholm


I'm attempting to do a multi-table delete against PostgreSQL (psycopg2) with 
the following query:


session.query(ProductionItem).\
filter(Project.id == ProductionItem.project_id,

   Project.code.in_(projects),

   ProductionItem.external_id.is_(None)).\

delete(synchronize_session=False)


But it produces incorrect SQL. PostgreSQL requires the following syntax for 
this query:


DELETE FROM production_items USING projects WHERE 
production_items.project_id = project.id AND project.code IN (...) AND 
production_items.external_id IS NONE


Instead, I get this:


DELETE FROM production_items WHERE production_items.project_id = project.id 
AND project.code IN (...) AND production_items.external_id IS NONE


At which point PG complains:


sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) missing 
FROM-clause entry for table "projects"


>From initial research this seems like a missing feature. Would it be 
possible to add this to the postgresql dialect somehow? I might be willing 
to contribute the code in that case.

-- 
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.