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.

can't use date type as primary key

See original GitHub issue

Bug 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:open
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
shankuljaincommented, Dec 24, 2021

FWIW, As mentioned in #10378 , the workaround is removing milliseconds.

async function create() {
  await prisma.test.create({data: {testId: 1, testDay: new Date(new Date().setMilliseconds(0))}})
}

I knew this is a solution. And I use moment().startOf(‘day’) to resolve this problem. But if the api is designed by this way, for developers, WTF…

Agreed, just wanted to put this so other folks don’t have to figure out themselves like I did.

1reaction
wvqcommented, Dec 22, 2021

If you close the issue, we won’t be able to track this and fix it. We are people and we make mistakes (or overlook details).

I’m sorry, commented yestoday, maybe click the wrong button.

Read more comments on GitHub >

github_iconTop 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 >

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