Fix unique constraint on review db #90
Labels
No labels
backend
beginner friendly
bug
chore
documentation
enhancement
frontend
needs-triage
no-stale
priority/high
priority/low
priority/medium
size/L
size/M
size/S
size/XL
size/XS
stage/backlog
stage/done
stage/in progress
stage/in-progress
stage/ready
stale
wontfix
needs/docs
needs/research
needs/upstream
type/bug
type/external
type/feature
type/refactor
urgency
high
urgency
immediate
urgency
low
urgency
medium
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ScottyLabs/housing#90
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Overall Objective
reviewTable.userIdinapps/backend/src/db/schema.tsis declared with.unique(). This makes it so that the database will reject any user's second review of any dorm. The constraint is on the user.What we actually want: a user may write one review per dorm, and many reviews across different dorms. That's a composite unique constraint on
(user_id, dorm_id).Suggested Approach
This should be a two-line schema change plus a generated migration. Since this is likely your first time working with our database I'll walk you through the whole Drizzle workflow you'll need for every other backend issue you will do in the future.
apps/backend/src/db/schema.tsand findreviewTableat the bottom..unique()from theuserIdcolumn definition. Leave.notNull().references(() => userTable.id)alone.You'll need to add
uniqueto the import list fromdrizzle-orm/pg-core. (copy the structure of the other imports)4. Generate the migration:
cd apps/backendthendeno task db:generate. This writes a new file intoapps/backend/drizzle/(it'll be0004_*.sql) and updatesdrizzle/meta/. These aren't junk, you'll want to commit all of it.5. Apply it locally with
deno task db:migrateand confirm it runs clean. If you have leftover data from experimenting,deno task db:studioopens a browser UI where you can look at the table.6. Sanity-check by hand in
db:studioorpsql: insert two rows with the sameuser_idbut differentdorm_id(should succeed), then two with the sameuser_idand samedorm_id(should fail).While you're in there:
reviewTableis also missing a default onsubmittedAt. Add.defaultNow()to it in the same PR. It's the same one-line-plus-migration pattern and it saves the reviews API from having to set the timestamp by hand.Help & Resources
Assigned to Loc