Discussion:
[sqlalchemy] Error while using return_defaults with insert
Christian Barra
2017-03-15 08:13:29 UTC
Permalink
Hello,

I am trying to fetch the result of a query after an insert.

SQLAlchemy 1.16 and PostgreSQL 9.6

This is the code:

engine = self.request.app['db']
async with engine.acquire() as conn:
query = models.User.insert().values(email=email, first_name=
first_name, last_name=last_name, password=password).return_defaults()
result = await conn.execute(query)

id = result.returned_defaults['id']
print(id)

ad this is the error:

id = result.returned_defaults['id']
AttributeError: 'ResultProxy' object has no attribute 'returned_defaults'

The example from the docs is the
same http://docs.sqlalchemy.org/en/rel_1_1/core/dml.html#sqlalchemy.sql.expression.ValuesBase.return_defaults,
apart from the async stuff
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
mike bayer
2017-03-15 13:37:23 UTC
Permalink
that's not SQLAlchemy, you're using this:

https://github.com/aio-libs/aiopg/blob/master/aiopg/sa/result.py#L205
Post by Christian Barra
Hello,
I am trying to fetch the result of a query after an insert.
SQLAlchemy 1.16 and PostgreSQL 9.6
engine = self.request.app['db']
query = models.User.insert().values(email=email, first_name=first_name, last_name=last_name, password=password).return_defaults()
result = await conn.execute(query)
id = result.returned_defaults['id']
print(id)
id = result.returned_defaults['id']
AttributeError: 'ResultProxy' object has no attribute 'returned_defaults'
The example from the docs is the
same http://docs.sqlalchemy.org/en/rel_1_1/core/dml.html#sqlalchemy.sql.expression.ValuesBase.return_defaults,
apart from the async stuff
--
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
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
Christian Barra
2017-03-21 21:20:06 UTC
Permalink
Thanks Mike,

at the end I resolved in this way:

async with engine.acquire() as conn:
query = models.User.insert().values(email=email, first_name=
first_name, last_name=last_name, password=password).return_defaults()
result = await conn.execute(query)
user_id = await result.scalar()
Post by mike bayer
https://github.com/aio-libs/aiopg/blob/master/aiopg/sa/result.py#L205
Post by Christian Barra
Hello,
I am trying to fetch the result of a query after an insert.
SQLAlchemy 1.16 and PostgreSQL 9.6
engine = self.request.app['db']
query = models.User.insert().values(email=email,
first_name=first_name, last_name=last_name,
password=password).return_defaults()
Post by Christian Barra
result = await conn.execute(query)
id = result.returned_defaults['id']
print(id)
id = result.returned_defaults['id']
AttributeError: 'ResultProxy' object has no attribute
'returned_defaults'
Post by Christian Barra
The example from the docs is the
same
http://docs.sqlalchemy.org/en/rel_1_1/core/dml.html#sqlalchemy.sql.expression.ValuesBase.return_defaults,
Post by Christian Barra
apart from the async stuff
--
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
<javascript:>
Post by Christian Barra
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...