-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix(prisma): add unique constraint to Chat model in Postgres #2247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(prisma): add unique constraint to Chat model in Postgres #2247
Conversation
Generated migration to add unique index on instanceId and remoteJid.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a missing unique constraint on (instanceId, remoteJid) for the Chat model in the PostgreSQL Prisma schema and introduces a corresponding migration to create the unique index in the database, aligning Postgres behavior with MySQL and fixing ON CONFLICT label operations. Sequence diagram for label operations using ON CONFLICT with the new Chat unique indexsequenceDiagram
actor "User" as User
participant "API Server" as Api
participant "Prisma Client" as Prisma
participant "PostgreSQL" as Postgres
"User" ->> "API Server": "Request to add or remove label on chat (instanceId, remoteJid)"
"API Server" ->> "Prisma Client": "call addLabel/removeLabel(chatId, labelId)"
"Prisma Client" ->> "PostgreSQL": "INSERT label mapping WITH ON CONFLICT (instanceId, remoteJid) DO UPDATE"
"PostgreSQL" ->> "PostgreSQL": "Use unique index \"Chat_instanceId_remoteJid_key\" to resolve conflict"
"PostgreSQL" -->> "Prisma Client": "Successful upsert of label mapping (no 42P10 error)"
"Prisma Client" -->> "API Server": "Operation result (success)"
"API Server" -->> "User": "Label operation completed successfully"
Entity relationship diagram for Chat model with composite unique constrainterDiagram
Chat {
string id
string instanceId
string remoteJid
string otherChatFields
string UNIQUE_instanceId_remoteJid
}
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes and found some issues that need to be addressed.
- Make sure the Prisma
Chatmodel inpostgresql-schema.prismaalso declares@@unique([instanceId, remoteJid])so that the schema definition remains consistent with the new database-level unique index. - Consider how this migration behaves on existing databases that may already contain duplicate
(instanceId, remoteJid)rows and, if necessary, add a pre-migration cleanup step or guard (e.g., deduplication or failing fast with a clear message) to avoid migration errors.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Make sure the Prisma `Chat` model in `postgresql-schema.prisma` also declares `@@unique([instanceId, remoteJid])` so that the schema definition remains consistent with the new database-level unique index.
- Consider how this migration behaves on existing databases that may already contain duplicate `(instanceId, remoteJid)` rows and, if necessary, add a pre-migration cleanup step or guard (e.g., deduplication or failing fast with a clear message) to avoid migration errors.
## Individual Comments
### Comment 1
<location> `prisma/postgresql-migrations/20251122003044_add_chat_instance_remotejid_unique/migration.sql:2` </location>
<code_context>
+-- AlterTable
+CREATE UNIQUE INDEX "Chat_instanceId_remoteJid_key" ON "Chat"("instanceId", "remoteJid");
</code_context>
<issue_to_address>
**issue (bug_risk):** Consider impact of existing duplicate rows before adding this UNIQUE index.
If any existing Chat rows share the same (instanceId, remoteJid), this migration will fail when the unique index is created. Please verify current data and, if needed, add a prior cleanup step to deduplicate or consolidate these rows so the migration succeeds in all environments.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
📋 Description
Fixes a critical bug affecting PostgreSQL users where label operations fail with
PrismaClientKnownRequestError(Code42P10).The issue occurs because
addLabelandremoveLabelmethods rely on anON CONFLICTclause that requires a unique constraint on(instanceId, remoteJid). This PR adds the missing@@uniqueconstraint to theChatmodel inpostgresql-schema.prismaand includes the necessary SQL migration to align behavior with MySQL.🔗 Related Issue
Closes #2152
🧪 Type of Change
🧪 Testing
Manually verified by applying the migration to a clean PostgreSQL container. Confirmed via
psqlthat the unique index"Chat_instanceId_remoteJid_key"was successfully created in the database structure.✅ Checklist
📝 Additional Notes
The migration file was generated using
prisma migrate dev --create-onlyand populated manually with the specificCREATE UNIQUE INDEXcommand. This ensures a clean migration path without generating unnecessary SQL for existing tables.Summary by Sourcery
Add a unique constraint for Chat records in the PostgreSQL schema to align behavior with other databases and prevent label operation failures.
Bug Fixes:
Build: