3. Updating a record
To update records in your database table you make use of the following functions:
update_by_pk()update_one()update_bulk()
1. update_by_pk()
The update_pk() method can be used as follows:
affected_rows = mysql_loom.update_by_pk(
instance=Post,
pk=1,
values=[
ColumnValue(name="title", value="Updated?"),
],
)
The above method takes in the following as arguments:
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class for which to update the instance. |
|
|
|
|
The primary key value of the instance to update. |
|
|
|
|
Single or Collection of |
|
|
2. update_one()
Here is an example illustrating how to use the update_one() method:
affected_rows = mysql_loom.update_one(
instance=Post,
filters=[
Filter(column="id", value=8, join_next_with="OR"),
Filter(column="userId", value=1, join_next_with="OR"),
],
values=[
ColumnValue(name="title", value="Updated?"),
],
)
The method takes the following as arguments:
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class for which to update the instance(s). |
|
|
|
|
Filter or collection of filters to apply to the update query. |
|
|
|
|
Single or collection of column-value pairs to update in the instance. |
|
|
3. update_bulk()
The update_bulk() method updates all records that match a filter in a database table.
affected_rows = mysql_loom.update_bulk(
instance=Post,
filters=[
Filter(column="id", value=8, join_next_with="OR"),
Filter(column="userId", value=1, join_next_with="OR"),
],
values=[
ColumnValue(name="title", value="Updated?"),
],
)
The above method takes in the following as argument:
Argument |
Description |
Type |
Default |
Required |
|---|---|---|---|---|
|
The model class for which to update instances. |
|
|
|
|
Filter or collection of filters to apply to the update query. |
|
|
|
|
Single or collection of column-value pairs to update in the instance. |
|
|