UNIQUE constraint in Yandex YDB SDK

147 Views Asked by At

how do I add a UNIQUE constraint to a table column when creating table using Yandex YDB SDK? I am using JavaScript version of the SDK, however, any slightest insight about how it is done on any version of the SDK would be much appreciated.

Searching for clues using "unique" keyword in https://github.com/ydb-platform/ydb-nodejs-sdk/tree/main/examples has returned no results.

2

There are 2 best solutions below

0
Pavel Shamal On

You don't need unique indexes in YDB. YDB operates at a serializable isolation level according to https://ydb.tech/en/docs/concepts/transactions, so you can simply check if a record with column value K exists, and if not, create a new one. If both operations (check and insert) are performed in one transaction, then it will be guaranteed that only one record with the value K will be inserted into the database.

0
kovalad On

Currently it's impossible to create a UNIQUE constraint in YDB Tables. It is not supported yet.

But this feature is listed in the roadmap as far as I can see