question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Potentially consolidate @id and @db.ObjectId

See original GitHub issue

Problem

Right now, it’s cumbersome to add @id and @db.ObjectId.

model User {
  id    String @id @default(dbgenerated()) @map("_id") @db.ObjectId
  name  String
}

To quote one of our early users:

The only thing that I didn’t understand was why is @db.ObjectId required in the schema when I already wrote @id

The reason for this is to keep the concepts orthogonal:

  • @id means primary key (not null + unique)
  • @db.ObjectId tells you that it’s a specific native type (BSON ObjectID type)

This is quite nuanced and we should think about ways to consolidate this for at least the common cases.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
dpetrickcommented, Jul 16, 2021

Our stance on the current iteration of the schema language is pretty firm in reducing magic opinionations. The DSL should be consistent for a broad variety of connectors with no need to learn intricacies and caveats when using different connectors. There is 100% a discussion to be had about manageability of the schema when it gets unwieldy like id String @id @default(dbgenerated()) @map("_id") @db.ObjectId.

why isn’t it required to specify INT as a type for the SQL databases? like in PG you can set ID type to string. Prisma just assumes that a default type is an integer. why then it can’t assume that the default type for Mongo id is ObjectID? just use it as a default, but allow people to override it if they wanna use any other type

This would be inconsistent with the DSL type representation and I hope the explanation above sheds some light on this. A string is a string and nothing specialized like an ObjectID, this default is consistent in the Prisma world.

If you are really unhappy about the current ergonomics of the Mongo ID, then I propose you just alias it and be done with it (yes, this exists and works already):

type MongoID = String @id @default(dbgenerated()) @map("_id") @db.ObjectId

model User {
  id   MongoID
  name String
}

The above is an undocumented schema feature and we will make breaking changes to this in the future.

2reactions
matthewmuellercommented, Mar 16, 2022

We’re going to close this since we do want to keep the concepts orthogonal.

In the future, I could see us supporting aliases, similar to: https://github.com/prisma/prisma/issues/6839#issuecomment-875423822 but not overriding existing attributes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practice for using _id for natural PKs - M320
the _id field contains a machine number + process id hash which means that if there's a requirement to merge documents or collections...
Read more >
How join two collections between Objects "_id" in MongoDB
$lookup , pass localField as category and pass foreignField as _id , there is no need to concat collection name .collection(collection).
Read more >
Does the OBJECTID in an Oracle Geodatabase table have ...
No, OBJECTID does not have a database level Primary Key constraint. ... But maintaining referential integrity when a unique ID splits is complicated...
Read more >
Usage of ObjectId() in MongoDB with Examples
This tutorial will explain you all about ObjectId() in MongoDB. Object ID is treated as a primary key within the Mongo DB collection...
Read more >
How is an object ID generated in MongoDB?
ObjectIds are actually auto-created by the driver (Java, PHP, Ruby, etc.) before being sent to the server. So the database itself does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found