Repository navigation
identity: DB unique (university_id, student_code) for student profiles - #101
Merged
qonnow merged 5 commits intoNov 21, 2026
Merged
Conversation
Per the PM decision on 2026-11-22, back the API-level duplicate check for advisor-created students with a database constraint so concurrent requests can't both insert the same student code in one university. - student_profiles_identity gets university_id (copy of users.university_id, FK universities), backfilled in the migration then made NOT NULL, plus UNIQUE(university_id, student_code). The migration fails with a clear message (no auto-fix) if any profile has no university or duplicates exist. - createStudent sets universityId on the profile; a P2002 on that constraint maps to 409 STATE_CONFLICT / STUDENT_CODE_ALREADY_USED. The existing API check stays. - Tests: same code in another university is allowed, raw duplicate insert hits P2002, and 6 concurrent creates give one 201 and five 409s. - docs: data-schema and ER diagram. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…y-rate-limit' into feature/identity-student-code-unique-per-university
qonnow
deleted the
feature/identity-student-code-unique-per-university
branch
November 21, 2026 20:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to PR #99, as the PM decided on 2026-11-22: keep the API-level duplicate check and add a DB unique constraint in a separate PR. This branch is based on
mainand does not depend on #99.Duplicate check before the migration
I ran the check query (below) against the local Docker PostgreSQL.
identity_dbthere has no tables or data: it was never migrated, so there are 0 duplicates and 0 student profiles without a university. I could not reach any staging or production database. Run the query on every real environment before deploying. The migration also guards itself. If any profile hasusers.university_id IS NULL, or any(university_id, student_code)pair is duplicated, it fails withRAISE EXCEPTIONand the row count. It never fixes data on its own.Changes
student_profiles_identitygets auniversity_id uuid NOT NULLcolumn (FKuniversities, ON DELETE RESTRICT). The migration backfills it fromusers.university_idbefore setting NOT NULL, then addsUNIQUE(university_id, student_code)(student_profiles_identity_university_id_student_code_key).POST /advisor-links/students(createStudent) setsuniversityIdon the profile. This is the only place insrcthat creates a student profile. The existing API check stays. A P2002 on this constraint now maps to409 STATE_CONFLICT/STUDENT_CODE_ALREADY_USED, as indocs/api-contract.md. Before this change it would have been reported asEMAIL_ALREADY_REGISTERED.users.university_id, so the copied column can't drift, and a deleted student's code can be reused.docs/data-schema.mdand the ER diagram indocs/diagram.md.Tests (Identity, real PostgreSQL in Docker)
advisor-link.spec.ts:409 STUDENT_CODE_ALREADY_USED, with exactly 1 row left.field: 'email'), which shows the backstop is what handles it.universityId.mocha: 77 passing, 3 runs in a row.tsc(src and test) andeslintpass.@nestjs/core12 (ERR_MODULE_NOT_FOUNDforfast-safe-stringify). That problem is not caused by this PR.