I have a table in by database that has a name field with a non-null and unique constraint.
On the frontend, a user can clone certain entities. In this scenario, the name field gets suffixed with a version number.
For instance, if record A exists with the name TEST_NAME, cloning this record would result in record B being created with the name TEST_NAME [2]. Cloning record A again would result in a record C with the name TEST_NAME [3].
To determine the version number, I run a count(*) against the table, returning the number of records that match the root name, in this case: 'TEST_NAME'.
Here is the query:
SELECT COUNT(*)
FROM my_table
WHERE name LIKE 'TEST_NAME%'
The issue here, is that if a user changes the name of record C to TEST_NAME [3]abc, then the above query would still pick this up and create record D with the name TEST_NAME [4], instead of TEST_NAME [3]abc [2].
How can I avoid this? I'd like to only match name values that follow the format ^TEST_NAME [x]$, where x is any integer.