How can I accomplish this in a schema?

I'm trying to make it so only one record with this itemId can have the status "ACTIVE" at a time. Something like this: ALTER TABLE ItemMintPermission ADD COLUMN ActiveItemID AS (CASE WHEN status='ACTIVE' THEN itemId ELSE NULL END) VIRTUAL; ALTER TABLE ItemMintPermission ADD UNIQUE INDEX idx_unique_active_item(ActiveItemID);
8 Replies
focused_morning
focused_morningOP14mo ago
its mysql btw
Angelelz
Angelelz14mo ago
There are hacky ways to do generated columns, like I show here: https://discord.com/channels/1043890932593987624/1156712008893354084/1156716755125796934 Basically you put whatever you want the SQL to have after the column name in the dataType method
focused_morning
focused_morningOP14mo ago
seems to not like virtual keyword
Angelelz
Angelelz14mo ago
isn't it virtual by default? At least in Mysql it is
focused_morning
focused_morningOP14mo ago
so I can exclude that keyword, im not 100% sure on that.
Angelelz
Angelelz14mo ago
The default is VIRTUAL if neither keyword is specified.
https://dev.mysql.com/doc/refman/8.0/en/create-table-generated-columns.html
focused_morning
focused_morningOP14mo ago
I keep getting this error: code: 'ER_PARSE_ERROR', errno: 1064, sql: "ALTER TABLE ItemMintPermission ADD activeItemId AS (CASE WHEN status='ACTIVE' THEN itemId ELSE NULL END);", but the sql looks right to me
Want results from more Discord servers?
Add your server