Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Id Strategies + Sequences #176

Closed
Closed

Description

@mavilein

Currently we map all fields of type Int @id always to an int column that is backed by a sequence. Currently it is not possible to have an int column as primary key that is not backed by a sequence.

Example:
The following example currently always maps to an int id field that is backed by a sequence.

model User {
  id Int @id
}

The spec for PSL does not say anything about the desired behavior in this case at all. We need this to start work on the related epic.

Activity

  1. added
    kind/spec
    engineering/blocked
    spec/change
    on Sep 27, 2019
  2. matthewmueller commented on Oct 4, 2019

    @matthewmueller
    Contributor

    matt's note: @id jumps to definition postgres.prisma

  3. matthewmueller commented on Oct 10, 2019

    @matthewmueller
    Contributor

    Screen Shot 2019-10-10 at 11 24 53 AM

    Screen Shot 2019-10-10 at 11 23 30 AM

    Screen Shot 2019-10-10 at 11 25 02 AM

  4. matthewmueller commented on Oct 18, 2019

    @matthewmueller
    Contributor

    I'm in favor of @mavilein's proposal where @id

    implies:

    • not null
    • unique

    does not imply:

    • autoincrement
    • there's a default value

    One consideration according to @do4gr: SQLite allows for null primary keys. I don't think this is a use-case we want to support, but if there are existing SQLite databases in the wild with null primary keys, we may run into problems.

  5. mavilein commented on Oct 18, 2019

    @mavilein
    ContributorAuthor

    @matthewmueller : Have we ever thought of @id @default(autoIncrement()) instead of @id @pg.serial. Would be kind of nice if all the auto generated id stuff would be handled through @default(...).

  6. matthewmueller commented on Oct 18, 2019

    @matthewmueller
    Contributor

    Interesting idea! Two questions:

    1. What would happen if I pass it a NULL value during an update?

    photon.users.find({ id: 10 }).update({ id: null })

    Would it maintain the same autoincremented value or would it take the next value?

    2. Does it work for other types?

    model User {
      createdAt DateTime default(autoincrement())
    }
  7. mavilein commented on Oct 18, 2019

    @mavilein
    ContributorAuthor

    @matthewmueller :

    1. What would happen if I pass it a NULL value during an update?

    Hmm not sure. We would need to define what @default exactly does. My understanding so far was that it just kicks in on creates where the value is not provided by the user.

    1. Does it work for other types?

    No only for Ints i guess. I would not know what to expect from incrementing a DateTime. One more millisecond, second, hour, day, year? 🤷‍♂

  8. added
    process/candidate
    on Oct 18, 2019
  9. added this to the Preview 16 milestone on Oct 28, 2019
  10. 35 remaining items

  11. schickling commented on Dec 4, 2019

    @schickling
    Member

    Looks mostly good from my side as well. 👍

    My biggest point of feedback would be that we should try to draw a clearer line (both in terms of concepts and syntax) when a feature is implemented on the Prisma level vs when a DB feature is used (See #305).

  12. sorenbs commented on Dec 5, 2019

    @sorenbs
    Member

    Thanks Matt!

    As I answer each question separately, keep in mind that we are working towards a launch of Photon in early Q1 2020, and explicitly accepting that some questions will have to be left unanswered for the time being.

    1) I can imagine a world where we'd provide Prisma-level sequences using an atomic counter.

    This is far in the future, and we will have to revisit the schema when we introduce this feature

    2) @default(dbProvided()) would not allow us to to spin up a replica of an existing database from a Prisma schema

    This is accurate. We will need to introduce extra syntax when we work towards the Lift release following the Photon release.

    3) Is it possible to have @default(dbProvided()) on something other than an @id?

    Yes. The presence of a database assisted default value on a required field simply makes the input field optional.

    4) What does the transition look like to migrate from @default(uuid()) to @default(dbProvided())? How about the other way?

    If you have a schema like this

    model User {
      id String @default(uuid())
    }
    

    and change it to this:

    model User {
      id String @default(dbProvided())
    }
    

    then queries that create a User without explicitly setting the id will fail.

    Once you migrate the database to actually have a default value for the id field, the query will start working again. You can perform this transition without disrupting your application by migrating the database first.

    5) Do you anticipate a future where we transition to native default values for databases that support it?

    I think this will be a different syntax. We will not explore this further before the initial Photon release.

  13. sorenbs commented on Dec 5, 2019

    @sorenbs
    Member

    We have reached a decision on this issue.

    I will transform it into a Spec PR with the aim to have it merged early next week.

    @mavilein and @janpio - we can begin implementation.

  14. do4gr commented on Dec 12, 2019

    @do4gr
    Member

    One question that is not defined in the issue:

    Our old behaviour was that Int ids are auto incrementing and therefore cannot be provided by the user and are not part of the CreateInputType therefore. For CUIDs and UUIDs the user could provide them, even when they were defined as default (and only UUIDs were validated I think). Do we want to keep this behaviour? It seems not consistent atm.

    id Int @id @default(autoincrement()) # you cannot set it yourself
    id String @id @default(cuid())       # you can provide whatever string you want yourself
    id String @id @default(uuid())       # you can provide a valid UUID yourself
  15. sorenbs commented on Dec 12, 2019

    @sorenbs
    Member

    Do we want to keep this behaviour? It seems not consistent atm.

    No. We want it to be consistent. You should be able to provide your own id in all cases. Later we will introduce a way to make a field read-only, but that is orthogonal to this issue.

  16. schickling commented on Dec 12, 2019

    @schickling
    Member

    Related to this we also thought of introducing a @default(autoincrement(), writeable: false) feature in this issue.

  17. janpio commented on Mar 3, 2020

    @janpio
    Contributor

    What is left to do here @mavilein ?

  18. mavilein commented on Mar 20, 2020

    @mavilein
    ContributorAuthor

    This is done.

  19. added this to the Preview 25 milestone on Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/schemaengineering/blockedkind/specspec/change

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions