# Duplicate key value violates unique constraint "user\_device\_pkey"

**URL:** <https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112>\
**Category:** Runtime Framework\
**Tags:** server-framework\
**Created:** [April 2, 2024, 4:20am UTC](https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112 "2024-04-02T04:20:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hojat-shafiee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/hojat-shafiee/32/1956_2.png) [@hojat-shafiee](https://forum.heroiclabs.com/u/hojat-shafiee)\
**Post date:** [April 2, 2024, 4:20am UTC](https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112/1 "2024-04-02T04:20:00Z")

</div>

Server : nakama 3.21.1 use with docker compose  
Client: C# | Unity

Hello, I’m using nakama custom authentication for login with Phone number and OTP & i link device id with “SystemInfo.deviceUniqueIdentifier” C# code for next logins via unity, but i get this error on server:

```auto
postgres-1 | 2024-04-02 03:55:59.310 UTC [30] ERROR: duplicate key value violates unique constraint "user_device_pkey"
postgres-1 | 2024-04-02 03:55:59.310 UTC [30] DETAIL: Key (id)=(8a11ead71a22f44a2e8f0b426220a44e8e7762cf) already exists.
postgres-1 | 2024-04-02 03:55:59.310 UTC [30] STATEMENT: INSERT INTO user_device (id, user_id) VALUES ($1, $2)

```

please add device id server side check in nakama and throw this error to client side  
note: i can solve this simply by replacing “SystemInfo.deviceUniqueIdentifier” with “Guid.NewGuid()”

 ![Screenshot 2024-04-02 075433](https://us1.discourse-cdn.com/flex020/uploads/heroiclabs/original/2X/2/27855e58d7c0f9412ddf4b8a6561d3b736582894.png)

client side code:

```auto
public void LinkDevice()
{
    client = new Client(connectionData.Scheme, connectionData.Host, connectionData.Port, connectionData.ServerKey, UnityWebRequestAdapter.Instance);
    Client.LinkDeviceAsync(session, SystemInfo.deviceUniqueIdentifier);
}

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [April 2, 2024, 11:30am UTC](https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112/2 "2024-04-02T11:30:02Z")

</div>

The error indicates that the device is already registered to another user, you may need to remove it using unlinkDevice, or you can do it manually through the Nakama console on the respective user Authentication tab.

---

<div class="post-metadata">

**Author:** ![hojat-shafiee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/hojat-shafiee/32/1956_2.png) [@hojat-shafiee](https://forum.heroiclabs.com/u/hojat-shafiee)\
**Post date:** [April 2, 2024, 1:49pm UTC](https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112/3 "2024-04-02T13:49:41Z")

</div>

Thanks for your support  
I know about nakama console and unlink device, but nakama DID NOT CHECK this error and AND THIS IS A DATABASE ERROR showing on nakama logs only!

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [April 2, 2024, 3:57pm UTC](https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112/4 "2024-04-02T15:57:10Z")

</div>

The error is checked and should return an HTTP 409 status code, unless there’s a bug: [nakama/server/core\_link.go at master · heroiclabs/nakama · GitHub](https://github.com/heroiclabs/nakama/blob/master/server/core_link.go#L133).

The logged query is from the Postgres docker container itself.

---

<div class="post-metadata">

**Author:** ![hojat-shafiee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/hojat-shafiee/32/1956_2.png) [@hojat-shafiee](https://forum.heroiclabs.com/u/hojat-shafiee)\
**Post date:** [April 3, 2024, 2:11am UTC](https://forum.heroiclabs.com/t/duplicate-key-value-violates-unique-constraint-user-device-pkey/5112/5 "2024-04-03T02:11:56Z")

</div>

You are right, I apologize, this error throw on server side and i’m mistake on my client code
