# Dexie.ConstraintError

#### Inheritance Hierarchy

* [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
  * Dexie.DexieError
    * Dexie.ConstraintError

#### Description

A database operation was attempted that violates a constraint. For example, if you've defined a unique index "name" and put two objects in that table with the same name, the second put() will result in a constraint error

```javascript
const db = new Dexie('mydb');
db.version(1).stores({
    foo: "id, &name"
});

async function main () {
    try {
        await db.foo.put({id: 1, name: "foo"}); // ok
        await db.foo.put({id: 2, name: "bar"}); // ok
        await db.foo.put({id: 3, name: "bar"}); // will fail with ConstraintError
    } catch (e) {
        assert (e.name === "ConstraintError");
    }
}

main();

```

#### Sample using Promise.catch()

```javascript
doSomeDatabaseWork().then(result => {
    // Success
}).catch('ConstraintError', e => {
    // Failed with ConstraintError
    console.error ("Constraint error: " + e.message);
}).catch(Error, e => {
    // Any other error derived from standard Error
    console.error ("Error: " + e.message);
}).catch(e => {
    // Other error such as a string was thrown
    console.error (e);
});
```

#### Sample: switch(error.name)

```javascript
db.on('error', function (error) {
    switch (error.name) {
        // errnames.Constraint ==="ConstraintError"
        case Dexie.errnames.Constraint:
            console.error ("Constraint error");
            break;
        default:
            console.error ("error: " + e);
    }
});
```

#### Properties

| name    | Will always be Dexie.errnames.Constraint === "ConstraintError"                     |
| ------- | ---------------------------------------------------------------------------------- |
| message | Detailed message                                                                   |
| inner?  | Inner exception instance (if any)                                                  |
| stack   | Can be present if the error was thrown. If signaled, there wont be any call stack. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dexiejs.typogram.co/dexieerror/dexie.constrainterror.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
