SqlInsert
- class SqlInsert(table, alias=None)
Builds an INSERT query. Created via
SQL.insert(table, alias). Inherits fromSqlStatement(viaSqlQuery).- Parameters:
table – Target table name string.
alias – Optional table alias. Renders as
INSERT INTO table AS alias. Useful with ON CONFLICT DO UPDATE to reference the target table by alias.
In the examples below,
qbrefers to aSqlInsertinstance:qb = SQL.insert('users')
- column(column, value=NOT_SPECIFIED)
Add a single column/value pair, or a column name without a value (when using separate
values()calls).- Parameters:
column – Column name string.
value – The value — a Python object (parameterized via a placemarker), or a
SqlObjwhich is rendered as literal SQL. If omitted, only the column name is recorded.
qb.column('name', 'Alice') # sql: INSERT INTO users (name) VALUES (%s) # args: ['Alice'] # mogrified: INSERT INTO users (name) VALUES ('Alice') qb.column('created_at', SQL.cat('now()')) # INSERT INTO users (created_at) # VALUES (now()) -- no placemarker, literal SQL
- columns(*columns)
Set column names (without values). Use with
values()orvalues_list()to provide the values separately.- Parameters:
columns – One or more column name strings.
qb.columns('code', 'title', 'did')
- columns_kwargs(**kwargs)
Add multiple column/value pairs. Columns are sorted alphabetically by name.
- Parameters:
kwargs – Keyword arguments where keys are column names and values are the corresponding values.
qb.columns_kwargs(name='Alice', email='alice@example.com', age=30) # INSERT INTO users (age, email, name) # VALUES (%s, %s, %s) # args: [30, 'alice@example.com', 'Alice']
- values(*values)
Set values for a single row. Use with
columns()to specify column names separately, or withoutcolumns()for a VALUES-only insert.- Parameters:
values – One or more values — Python objects (parameterized) or
SqlObjinstances (rendered as literal SQL).
(SQL.insert('films') .columns('code', 'title') .values('T_601', 'Yojimbo')) # INSERT INTO films (code, title) # VALUES (%s, %s) # args: ['T_601', 'Yojimbo'] (SQL.insert('films') .values('UA502', 'Bananas', 105)) # INSERT INTO films # VALUES (%s, %s, %s) # args: ['UA502', 'Bananas', 105]
- values_list(values_list)
Set multiple rows of values. Each element is a tuple of values.
- Parameters:
values_list – A list of tuples, where each tuple contains values for one row.
(SQL.insert('films') .columns('code', 'title') .values_list([('B6717', 'Tampopo'), ('HG120', 'The Dinner Game')])) # INSERT INTO films (code, title) # VALUES # (%s, %s), # (%s, %s) # args: ['B6717', 'Tampopo', 'HG120', 'The Dinner Game']
- default_values()
Generate
INSERT INTO table DEFAULT VALUES.SQL.insert('films').default_values() # INSERT INTO films DEFAULT VALUES
- query(query)
Use a SELECT query as the value source (INSERT INTO … SELECT).
- Parameters:
query – A
SqlQuery(typicallySQL.select()).
(SQL.insert('archive') .query(SQL.select() .from_table('staging') .column('*'))) # INSERT INTO archive # SELECT * # FROM staging
- query_columns()
When using
query(), derive the INSERT column list from the query’s columns. Without this, no column list is emitted.(SQL.insert('archive') .query_columns() .query(SQL.select() .from_table('staging') .column('name') .column('email'))) # INSERT INTO archive (name, email) # SELECT name, email # FROM staging
- on_conflict(condition, action=None)
Add ON CONFLICT clause.
- Parameters:
condition – Conflict target — a string (e.g.
'(email)'or'ON CONSTRAINT pk_name') or aSqlObjfor complex targets (e.g.SQL.cat('(did)\nWHERE is_active')).action – A
SqlUpdate(created viaSQL.update()) for DO UPDATE, orNone(default) for DO NOTHING.
# DO NOTHING qb.on_conflict('(email)') # ON CONFLICT (email) DO NOTHING # DO UPDATE action = SQL.update().set('name', SQL.cat('excluded.name')) qb.on_conflict('(email)', action) # ON CONFLICT (email) DO UPDATE # SET name = excluded.name # Named constraint qb.on_conflict('ON CONSTRAINT distributors_pkey') # ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING
- returning(*columns)
Add a RETURNING clause.
- Parameters:
columns – One or more column name strings or
SqlObjinstances.
qb.returning('id', 'created_at') # RETURNING id, created_at
- recursive()
Mark the WITH clause as RECURSIVE.
- with_cte(cte)
Add a
SqlCteinstance to the WITH clause.- Parameters:
cte – A
SqlCteobject (created viaSQL.cte()).
qb.with_cte(SQL.cte('helper', helper_query))
- with_cte_query(alias, query)
Shorthand for adding a CTE.
- Parameters:
alias – CTE name string.
query – A
SqlQueryfor the CTE body.
qb.with_cte_query('upd', update_query)