Skip to content

Rethink Unique #1239

@parsonsmatt

Description

@parsonsmatt

The issue in #1236 illustrates a need to rethink how we handle Uniques.

Ultimately, the problem comes down to conflating two uses of the type:

  1. Specifying a unique key for retrieval (eg getBy :: Unique rec -> m (Maybe (Entity rec)))
  2. Specifying a uniqueness constraint for conflict (eg upsertBy).

The EntityField rec typ type works great for talking about the fields on an entity, so it seems like we might copy that design.

-- let's factor this out of PersistEntity while we're at it
class PersistEntity rec => PersistEntityUnique rec where
    data UniqueConstraint rec :: Type -> Type

data Unique rec where
    Unique :: UniqueConstraint rec typ -> typ -> Unique rec

it would be nice if we could somehow capture the actual EntityFields in the UniqueConstraint type, too. Then we could reuse things like persistFieldDef :: EntityField rec typ -> FieldDef. But, we can't promote data family constructors, as that "requires dependent types" for some reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions