# upsert Parameter

> Insert the row when the id does not exist with upsert=true on update by id and replace by id in API Maker, on every database.

Source: https://docs.apimaker.dev/v1/docs/apis-all/query-params/upsert.html

`upsert=true` makes [update by id](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-update-by-id-api.html) and [replace by id](https://docs.apimaker.dev/v1/docs/apis-all/schema-apis/auto-generated-schema-based-replace-by-id-api.html) insert a row when the id does not exist. Without it (the default, `false`), an unknown id changes nothing and answers `data: null`.

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

**Body**

```json
{ "first_name": "John", "last_name": "Sina", "pincode": 382350, "isActive": 0 }
```

| Row 999 | What happens | Answer |
|---|---|---|
| exists | updated with the body | the row after the change |
| does not exist | inserted with `customer_id: 999` and the body | the new row |

- The inserted row is validated as a save : with a schema, every `required` field must be in the body.
- Works on MongoDB, MySQL, MariaDB, PostgreSQL, SQL Server and Oracle.
- With `returnDocument=before` and an insert, the answer is `null` : there was no row before.
- From code : `g.sys.db.updateById({ ..., upsert: true })`.

- [returnDocument →](https://docs.apimaker.dev/v1/docs/apis-all/query-params/returnDocument.html)
- [getTotalCount](https://docs.apimaker.dev/v1/docs/apis-all/query-params/getTotalCount.html)
