Making a key in a object with jsonb unique

type Wallets = {
wallet_address: string;
blockchain: string;
};

export const user = pgTable(
'user',
{
id: serial('id').primaryKey(),
wallets: jsonb('wallets').$type<Wallets[]>(),
);
type Wallets = {
wallet_address: string;
blockchain: string;
};

export const user = pgTable(
'user',
{
id: serial('id').primaryKey(),
wallets: jsonb('wallets').$type<Wallets[]>(),
);
how would i make wallet_address unique?
3 Replies
rphlmr ⚡
rphlmr ⚡2mo ago
Technically you can't. Your best option is to have an other table with wallet_address as primary key and userId as foreign key (1 user can have many wallets but a given wallet has only one owner).
rphlmr ⚡
rphlmr ⚡2mo ago
Then, query api will help to grab everything together: https://orm.drizzle.team/docs/rqb
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
HotBBQSauce
HotBBQSauce3w ago
@Raphaël M (@rphlmr) ⚡ is right, however technically you can build your own sql query that will only allow the insertion of unique wallet objects based on a predefined key.