Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pocketbase API Rule to only create record if there isn't already a record with the same values

Tags:

pocketbase

Is there a way to create an API rule that only allows the user to create a record if there isn't already one with the same combination of values. I want it to be possible to have a record with the values "a" and "b" and also one with the values "a" and "c" but not another one with the values "a" and "b".

I tried to do something like

NOT IN (SELECT ... FROM table WHERE value_1="a")
like image 503
wafgawfwaf Avatar asked Oct 31 '25 12:10

wafgawfwaf


1 Answers

The most straightforward way seems to be to create a new index on those columns in the edit collection form and check the "Unique" toggle in the new index form.

TL;DR

Click on the collection you want to edit: Opening the New/Update Index form

Select "New Index" under unique constraints and indexes. In the New/Update Index Modal, you can toggle "Unique" and select from a list of presets (columns) to add them to the index. Selecting the name and avatar column for an index

See this comment from ganigeorgiev on github: https://github.com/pocketbase/pocketbase/issues/544#issuecomment-1477895278-permalink

like image 126
Todd Avatar answered Nov 02 '25 12:11

Todd