can't use date type as primary key
See original GitHub issueBug description
If mysql date type field is not a primary key, the create function will success. But define a Composite Primary Key with Integer and Date, will cause an error
How to reproduce
CREATE TABLE `test` (
`test_id` int(11) NOT NULL,
`test_day` date NOT NULL,
PRIMARY KEY (`test_id`, `test_day`) USING BTREE
);
import prisma from './src/prisma'
async function create() {
await prisma.test.create({data: {testId: 1, testDay: new Date()}})
}
create()
output:
BEGIN []
INSERT INTO `testdb`.`test` (`test_id`,`test_day`) VALUES (?,?) [1,2021-08-27 16:00:00 UTC]
SELECT `testdb`.`test`.`test_id`, `testdb`.`test`.`test_day` FROM `testdb`.`test` WHERE (`testdb`.`test`.`test_id` = ? AND `testdb`.`test`.`test_day` = ?) LIMIT ? OFFSET ? [1,2021-08-27 16:00:00 UTC,1,0]
ROLLBACK []
BEGIN []
INSERT INTO `testdb`.`test` (`test_id`,`test_day`) VALUES (?,?) [1,2021-08-27 16:00:00 UTC]
SELECT `testdb`.`test`.`test_id`, `testdb`.`test`.`test_day` FROM `testdb`.`test` WHERE (`testdb`.`test`.`test_id` = ? AND `testdb`.`test`.`test_day` = ?) LIMIT ? OFFSET ? [1,2021-08-27 16:00:00 UTC,1,0]
ROLLBACK []
BEGIN []
INSERT INTO `testdb`.`test` (`test_id`,`test_day`) VALUES (?,?) [1,2021-08-27 16:00:00 UTC]
SELECT `testdb`.`test`.`test_id`, `testdb`.`test`.`test_day` FROM `testdb`.`test` WHERE (`testdb`.`test`.`test_id` = ? AND `testdb`.`test`.`test_day` = ?) LIMIT ? OFFSET ? [1,2021-08-27 16:00:00 UTC,1,0]
ROLLBACK []
/Users/wis/Workspaces/testdb/testprisma/node_modules/@prisma/client/runtime/index.js:36437
throw new import_engine_core3.PrismaClientUnknownRequestError(message, this.client._clientVersion);
^
PrismaClientUnknownRequestError3 [PrismaClientUnknownRequestError]:
Invalid `prisma.test.create()` invocation:
Query createOneTest is required to return data, but found no record(s).
at RequestHandler.request (/Users/wis/Workspaces/testdb/testprisma/node_modules/@prisma/client/runtime/index.js:36437:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
clientVersion: '2.30.0'
}
Expected behavior
No response
Prisma information
generator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Test {
testId Int @map("test_id")
testDay DateTime @db.Date @map("test_day")
@@id([testId, testDay])
@@map("test")
}
Environment & setup
- OS: Mac OS
- Database: MySQL
- Node.js version: v16.6.1
Prisma Version
prisma : 2.30.0
@prisma/client : 2.30.0
Current platform : darwin
Query Engine (Binary) : query-engine 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 60b19f4a1de4fe95741da371b4c44a92f4d1adcb
Studio : 0.422.0
Preview Features : interactiveTransactions
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Date Field as Primary Key - Ask TOM
Sometimes a column with 'date' Datatype needs to be part of Primary Key or Unique Key. ... BY DEFINITION you cannot use SYSDATE...
Read more >Pros & Cons of Date Column as Part of Primary Key
Your choices here are to use a surrogate key, use a DATETIME instead of DATE (will give you more margin for updates), or...
Read more >When deciding a Primary Key, can I use Date part of a ...
You can use a date field as a primary key, but you can't key on PART of a field. You'd have to make...
Read more >Using Datetime Columns as Primary Keys - ITPro Today
A basic database-design principle is that a primary key must always be unique. And because SQL Server can't differentiate between datetime values that...
Read more >What is a key? - AppSheet Help - Google Support
Types of keys; Select a key; Column types that can't be used as the key ... For example, an app formula that includes...
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
Agreed, just wanted to put this so other folks don’t have to figure out themselves like I did.
I’m sorry, commented yestoday, maybe click the wrong button.