Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit f5d94cd

Browse files
authored
feat: support multi-row inserts (#671)
Support multi-row inserts like ```sql INSERT INTO tbl VALUES ('a'), ('b') ``` fixes: #670
1 parent 6ae9bf2 commit f5d94cd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ class SpannerDialect(DefaultDialect):
807807
insert_returning = True
808808
update_returning = True
809809
delete_returning = True
810+
supports_multivalues_insert = True
810811

811812
ddl_compiler = SpannerDDLCompiler
812813
preparer = SpannerIdentifierPreparer

test/system/test_basics.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,32 @@ class SchemaUser(Base):
209209
select(SchemaUser).where(SchemaUser.name == "NewName")
210210
).all()
211211
eq_(0, len(users))
212+
213+
def test_multi_row_insert(self, connection):
214+
"""Ensures we can perform multi-row inserts."""
215+
216+
class Base(DeclarativeBase):
217+
pass
218+
219+
class User(Base):
220+
__tablename__ = "users"
221+
ID: Mapped[int] = mapped_column(primary_key=True)
222+
name: Mapped[str] = mapped_column(String(20))
223+
224+
with connection.engine.begin() as conn:
225+
inserted_rows = list(
226+
conn.execute(
227+
User.__table__.insert()
228+
.values([{"name": "a"}, {"name": "b"}])
229+
.returning(User.__table__.c.ID, User.__table__.c.name)
230+
)
231+
)
232+
233+
eq_(2, len(inserted_rows))
234+
eq_({"a", "b"}, {row.name for row in inserted_rows})
235+
236+
with connection.engine.connect() as conn:
237+
selected_rows = list(conn.execute(User.__table__.select()))
238+
239+
eq_(len(inserted_rows), len(selected_rows))
240+
eq_(set(inserted_rows), set(selected_rows))

0 commit comments

Comments
 (0)