Potentially consolidate @id and @db.ObjectId
See original GitHub issueProblem
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:
- Created 2 years ago
- Reactions:4
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
.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):
The above is an undocumented schema feature and we will make breaking changes to this in the future.
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.