MCPcopy Index your code
hub / github.com/sqlalchemy/sqlalchemy / insert_values

Method insert_values

test/sql/test_insert_exec.py:140–181  ·  view source on GitHub ↗

Inserts a row into a table, returns the full list of values INSERTed including defaults that fired off on the DB side and detects rows that had defaults and post-fetches.

(table_, values)

Source from the content-addressed store, hash-verified

138 """Tests the inserted_primary_key and lastrow_has_id() functions."""
139
140 def insert_values(table_, values):
141 """
142 Inserts a row into a table, returns the full list of values
143 INSERTed including defaults that fired off on the DB side and
144 detects rows that had defaults and post-fetches.
145 """
146
147 # verify implicit_returning is working
148 if (
149 connection.dialect.insert_returning
150 and table_.implicit_returning
151 and not connection.dialect.postfetch_lastrowid
152 ):
153 ins = table_.insert()
154 comp = ins.compile(connection, column_keys=list(values))
155 if not set(values).issuperset(
156 c.key for c in table_.primary_key
157 ):
158 is_(bool(comp.returning), True)
159
160 result = connection.execute(table_.insert(), values)
161 ret = values.copy()
162
163 ipk = result.inserted_primary_key
164 for col, id_ in zip(table_.primary_key, ipk):
165 ret[col.key] = id_
166
167 if result.lastrow_has_defaults():
168 criterion = and_(
169 *[
170 col == id_
171 for col, id_ in zip(
172 table_.primary_key, result.inserted_primary_key
173 )
174 ]
175 )
176 row = connection.execute(
177 table_.select().where(criterion)
178 ).first()
179 for c in table_.c:
180 ret[c.key] = row._mapping[c]
181 return ret, ipk
182
183 table_.create(connection, checkfirst=True)
184 i, ipk = insert_values(table_, values)

Callers

nothing calls this directly

Calls 11

is_Function · 0.90
and_Function · 0.90
insertMethod · 0.45
compileMethod · 0.45
issupersetMethod · 0.45
executeMethod · 0.45
copyMethod · 0.45
lastrow_has_defaultsMethod · 0.45
firstMethod · 0.45
whereMethod · 0.45
selectMethod · 0.45

Tested by

no test coverage detected