Re: [sqlalchemy] Using sql expression to update a column from another table's column

2017-05-10 Thread mike bayer

we don't support the INNER JOIN keyword here, use an implicit join:

s.query(A).filter(A.id == B.id).filter(B.typ == '2').update({A.str: 
func.concat(A.str, B.str)}, synchronize_session=False)





On 05/10/2017 11:30 AM, Shane Carey wrote:
Given the following declarative schema, how can I generate the UPDATE 
statement? I am using MySQL


class A(Base):
 __tablename__ = 'a'
 id = Column(Integer, primary_key=True)
 str = Column(String(16), nullable=False)

 bs = relationship(B)

class B(Base):
 __tablename__ = 'b'
 id = Column(Integer, primary_key=True)
 a_id = Column(Integer, ForeignKey(A.id), nullable=False)
 typ = Column(Enum('1', '2'), nullable=False)
 str = Column(String(16), nullable=False)

 __table_args__ = ( UniqueConstraint(a_id, typ), )

UPDATE a
INNER JOIN b ON b.a_id = a.id
SET a.str = CONCAT(a.str, b.str)
WHERE b.typ = '2';

I have tried

session.query(A).join(B).filter(B.typ == '2').update({ 'str': 
func.concat(A.str, B.str) }, synchonize_session=False)


but this raises an exception

s.execute(update(A).join(B, b.a_id == A.id).where(B.typ == 
'2').values(str=func.concat(A.str, B.str)))


does not work because join is not a member of Update

How can I achieve this? It would be especially nice if I could use the 
session to do so rather than raw sql expression



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


[sqlalchemy] Using sql expression to update a column from another table's column

2017-05-10 Thread Shane Carey
Given the following declarative schema, how can I generate the UPDATE 
statement? I am using MySQL

class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
str = Column(String(16), nullable=False)

bs = relationship(B)

class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey(A.id), nullable=False)
typ = Column(Enum('1', '2'), nullable=False)
str = Column(String(16), nullable=False)

__table_args__ = ( UniqueConstraint(a_id, typ), )

UPDATE a
INNER JOIN b ON b.a_id = a.id
SET a.str = CONCAT(a.str, b.str)
WHERE b.typ = '2';

I have tried

session.query(A).join(B).filter(B.typ == '2').update({ 'str': 
func.concat(A.str, B.str) }, synchonize_session=False)

but this raises an exception

s.execute(update(A).join(B, b.a_id == A.id).where(B.typ == 
'2').values(str=func.concat(A.str, B.str)))

does not work because join is not a member of Update

How can I achieve this? It would be especially nice if I could use the 
session to do so rather than raw sql expression


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