#Can a constraint ensure that no more than (say) 10 rows have a particular attribute?

13 messages · Page 1 of 1 (latest)

south hedge
#

Specifically, I have a Player model with a boolean field named allow_bot_to_play_for_me. I'd like to ensure that no more than 10 Players have this field set to True.

Of course I could do this in the "save" method, but if it's possible, I'd prefer to have a constraint do this. But I can't figure out if it's possible, nor how I'd do it if so.

distant sage
#

Errmm... Only solution I can think of is on a trigger level.

south hedge
#

heh, I didn't mention that I'm using Postgres, but I am.

#

Those partial-unique indices make my brain hurt

distant sage
south hedge
#

yeah I've seen that

#

why do I get the sense you want me to be the guinea pig 🤣

distant sage
#

subtelty is not my strong suit XD

south hedge
#

srsly this is something I only want to do for now, to protect my server from too many clients; ideally I'll come up with a better way (like, making the server efficient enough that it can handle a lot of clients), so I don't want anything too heavy. Triggers do feel like "too heavy" 😐

distant sage
#

They do feel unnatural, I'd probably not use them in such a scenario as well.

south hedge
#

in a perfect world, I'd play with 'em to get a sense of how they work, what they're good at (and what they're not good at), &c. Always nice to have more tools in the box. Alas, the world is not perfect.

true bronze
#

I have found triggers to be good when calculating stuff that is effectively read only to everything else.

If they update columns that can also be updated elsewhere you can get a world of painful bugs