Duplicate key value violates unique constraint "user_device_pkey"

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:

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()”

client side code:

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

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.

1 Like

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!

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.

The logged query is from the Postgres docker container itself.

1 Like

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

1 Like