-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
execute_many returns None after successful insert #82
Comments
execute and execute_many will always return None. If you want to retrieve values, you'll need to use the fetch_all or fetch_one methods with your insert statement. You'll also need to add a RETURNING clause on your SQL statement. For sqlalchemy, I typically use bindparams something similar to : stmt = sa.insert().values(
name=sa.bindparam('name'),
dsn=sa.bindparam('dsn')
).returning(clients.id)
values = [...]
results = await database.fetch_all(query=stmt, values=values)
for result in results:
print(result['name']) Your idea on a loop to iterate is correct in this packages current form. I haven't found a way to do a bulk add using named parameters. You can execute it with the execute_many with a list of values, and it'll do it in micro transactions, but you cannot get the results returned from that method. |
Has there been any work on this issue, or is inserting each object one by one in order to retrieve the respective IDs still the only way? |
@frjonsen yep, is still the only intended way. However, you've got the |
@vmarkovtsev Thank for the head's up about The solution we went for is this. Since our table looks like the follow: Foo= Table(
"foo",
metadata,
Column("id", Text, primary_key=True, default=generate_foo_id),
Column("owner_id", UUIDType, ForeignKey("user.id"), nullable=False),
Column("creator_id", UUIDType, ForeignKey("user.id"), nullable=False),
Column("used_at", DateTime(timezone=True)),
Column("notes", Text),
Column("created_at", DateTime(timezone=True), default=func.now(), nullable=False),
Column("updated_at", DateTime(timezone=True), default=func.now(), onupdate=func.now(), nullable=False),
) where generate_foo_id is a function we use for generating a unique string (because of business logic reasons we can't use an UUID here), we opted for generating all the defaults ourselves before inserting, i.e. we insert using:
This isn't exactly ideal, since the table defines using |
Of course, it gives you direct access to the underlying DB driver. For example, it is asyncpg for Postgres, and it has |
I have the following table
should I perform this:
then I got rq2 that is equal to the id of the row inserted
now if I try to do
rows are indeed correctly inserted, but I don't have the ids of the rows inserted, which sucks because they are foreign keys in another table I plan on using for further operations
question is: is that intended / normal in which case I'm ok to make a loop on the values I want to insert, grab the id inserted and deal with it later or is there a way to enhance execute_many ?
The text was updated successfully, but these errors were encountered: