# Update by ID API (generated)

> Change some fields of one row with the generated update by id API of API Maker - by primary key or any column, with upsert, returnDocument, select and deep, and the version check of optimistic concurrency control.

Source: https://docs.apimaker.dev/v1/docs/apis-all/generated-apis/auto-generated-update-by-id-api.html

Changes the fields you send on one row and leaves the others alone. The row is found by its primary key, or by the column you name.

| | |
|---|---|
| Method | PUT |
| URL | `/api/gen/admin/mysql8/inventory/customers/update-by-id/:id[/:primaryKey]` |
| Body | the fields to change |
| Query params | [select](https://docs.apimaker.dev/v1/docs/apis-all/query-params/select.html), [deep](https://docs.apimaker.dev/v1/docs/apis-all/query-params/deep.html), [upsert](https://docs.apimaker.dev/v1/docs/apis-all/query-params/upsert.html), [returnDocument](https://docs.apimaker.dev/v1/docs/apis-all/query-params/returnDocument.html), throwErrorIfRecordNotFound, find |
| Answer | `data` : the row after the change (or before, with `returnDocument=before`) |
| Databases | MongoDB, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle, TiDB, Percona |
| Cached | no (a write ; it resets the cache of the table) |
| From code | [`g.sys.db.gen.updateByIdGen`](https://docs.apimaker.dev/v1/examples/sys/db/gen/updateByIdGen.html) |
| API id | `GEN_PUT_UPDATE_BY_ID` (groups, settings, hooks, WebSocket subscriptions) |
| The other family | [Update by id as a schema API](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-update-by-id-api.html) |

> **Schemaless : the data passes as it is**
>
> The generated APIs never read the [schema](https://docs.apimaker.dev/v1/docs/schema/schema.html) of the table. Nothing is converted, validated, defaulted, encrypted or numbered : what you send is what the database gets, and a filter matches the type you send (`"22"` does not match the number `22`). On MongoDB, strings which look like ObjectIds are converted. Every table has these APIs, with or without a schema ; the same operations under `/api/schema` apply the schema.

## The URL

`/api/gen/admin/mysql8/inventory/customers/update-by-id/:id[/:primaryKey]` : replace `admin` with the user path of your account, `mysql8` with your instance, `inventory` with the database and `customers` with the table. The examples use this table :

| customer_id | first_name | last_name | last_update           | pincode | isActive |
|-------------|------------|-----------|-----------------------|---------|----------|
| 1           | Bob        | lin       | 2022-11-14 04: 34: 58 | 382345  | 1        |
| 2           | Alice      | Page      | 2022-10-15 02: 10: 40 | 382346  | 1        |
| 3           | Mallory    | Brown     | 2022-09-13 03: 44: 05 | 382347  | 1        |
| 4           | Eve        | Mathly    | 2022-11-12 01: 59: 33 | 382348  | 1        |
| 5           | Eve        | Page      | 2022-11-12 01: 59: 33 | 382349  | 1        |

## Some fields

```text
PUT /api/gen/admin/mysql8/inventory/customers/update-by-id/1
```

**Body**

```json
{ "pincode": 382330 }
```

**Answer**

```json
{
    "success": true,
    "statusCode": 200,
    "data": { "customer_id": 1, "first_name": "Bob", "last_name": "lin", "last_update": "2022-11-14T04:34:58.000Z", "pincode": 382330, "isActive": 1 }
}
```

- Send only what changes. Several fields at once are fine. On MongoDB, a nested key like `"address.city"` updates that path.

## By another column

```text
PUT /api/gen/admin/mysql8/inventory/customers/update-by-id/Bob/first_name
```

- The row whose `first_name` is `Bob` is updated. The column named as the key can itself be in the body : `/update-by-id/Mallory/first_name` with `{ "first_name": "Alice" }` renames her.

## Insert when missing : upsert

```text
PUT /api/gen/admin/mysql8/inventory/customers/update-by-id/999?upsert=true
```

**Body**

```json
{ "first_name": "Daphney", "last_name": "Sonia", "pincode": 980220 }
```

- No row with `customer_id` 999 : one is inserted with the id and the body (the body is validated as a save). A row exists : it is updated. On every database.

## The row before the change

```text
PUT /api/gen/admin/mysql8/inventory/customers/update-by-id/1?returnDocument=before
```

- The answer holds the row as it was ; the update happens all the same. Default : `after`. See [returnDocument](https://docs.apimaker.dev/v1/docs/apis-all/query-params/returnDocument.html).

## Unknown id

- By default an unknown id answers `success: true` with `data: null` and changes nothing. With `?throwErrorIfRecordNotFound=true` it answers `404` and `Record not found.`
- `find` adds conditions the row must match : `?find={isActive:1}`. A pre hook can add the condition for every call.

## Fields and related rows in the answer

```text
PUT /api/gen/admin/mysql8/inventory/customers/update-by-id/1?select=first_name,last_name&deep=[{s_key:'customer_id',t_col:'orders',t_key:'customer_id'}]
```

## From your code

[`g.sys.db.gen.updateByIdGen`](https://docs.apimaker.dev/v1/examples/sys/db/gen/updateByIdGen.html) makes the same call from custom APIs, hooks, events, schedulers and test cases, with the same headers and params. Pass `true` as the second argument to get the whole [response envelope](https://docs.apimaker.dev/v1/docs/apis-all/response-format.html) instead of `data`.

## Headers

Every call takes the [request headers](https://docs.apimaker.dev/v1/docs/apis-all/header/requestHeader.html) : the tokens, `x-am-response-case`, `x-am-content-type-response` (JSON, XML, YAML…), `x-am-response-object-type: make_flat`, `x-am-cache-control`, `x-am-get-encrypted-data`, `x-am-internationalization`, `x-am-tenant-username`, `x-am-meta`.

## Errors

| Code | When |
|---|---|
| `401` | The token in `x-am-authorization` is missing or invalid. |
| `403` | No group of the API user grants this API of this table, or a field of the request. |
| `404` | The instance, database or table of the URL does not exist. |
| `400` | The query or the body is wrong : the [error messages](https://docs.apimaker.dev/v1/docs/apis-all/error-codes.html) say which key and why. |

## Related

- [All APIs at a glance](https://docs.apimaker.dev/v1/docs/apis-all/overview.html) · [Query params](https://docs.apimaker.dev/v1/docs/apis-all/query-params/query-params.html) · [Response format](https://docs.apimaker.dev/v1/docs/apis-all/response-format.html) · [Pre hooks](https://docs.apimaker.dev/v1/docs/apis-all/hooks/preHook-api.html) and [post hooks](https://docs.apimaker.dev/v1/docs/apis-all/hooks/postHook-api.html) · [Automatic caching](https://docs.apimaker.dev/v1/docs/features/automatic-caching.html)
