Skip to content

Composite "Primary" keys do not imply uniqueness leading to runtime exception #768

@hakujin

Description

@hakujin

Persistent version: 2.7.1 (lts-9.17)

Behavior

Explicit composite primary keys do not imply uniqueness, leading to runtime exceptions when attempting upserts. This behavior is unexpected when using the Postgres backend.

Expectation

Postgres primary keys imply uniqueness (or upsert's Unique dependency is verified at compile time).

Example

Model:

User sql=users
    Id UUID default=uuid_generate_v4
    email Email sqltype=text
    UniqueEmail email
    deriving Show Eq Typeable
Project json sql=projects
    Id UUID default=uuid_generate_v4
    name Text
    UniqueProject name
    deriving Show Eq Typeable
UserProjectVote sql=user_project_votes
    userId UserId
    projectId ProjectId
    vote Int
    UniqueUserProjectVote userId projectId -- this line is required
    Primary userId projectId
    deriving Show Eq Typeable

Yields this exception when the UniqueUserProjectVote line is omitted:

> upsert (UserProjectVote userId projectId 3) [UserProjectVoteVote +=. 1]
uncaught exception: IOException of type UserError (user error (getByValue: UserProjectVote does not have any Unique))

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions