INSERT INTO rehus.curriculumsfile (fileid, originalfilename, filename, path, curriculumid, userid, companyid) VALUES ((SELECT COALESCE(MAX(fileid), 0) + 1 FROM rehus.curriculumsfile WHERE curriculumid = $1 AND userid = $2 AND companyid = $3), $4, $5, $6, $1, $2, $3)
I'm trying to insert into the database file records as the upload concludes. But I'm receiving duplicated key erros, because the select is generating the same ID in multiple concurrent accesses. How can I solve this issue? And why does this happen? It's the same query, both insert and select statements should be resolved under the same "db status".
I know that sequences or series should solve the problem but they don't really work for me.