Error encountered: The input value does not correspond to any valid input type for the specified field in Prisma -Seed

When trying to run the seed command tsx prisma/seed.ts, it failed to create a post and returned an error.

→ 6 await prisma.habit.create( Validation failed for the query:

Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at 
Mutation.createOneHabit.data.HabitCreateInput.created_at
: A value is required but not set., Query parsing/validation error at 
Mutation.createOneHabit.data.HabitUncheckedCreateInput.created_at
: A value is required but not set.]
at Mutation.createOneHabit.data at Object.handleRequestError (/Users/luistigre/www/nlw/nlw/aulas/server/node_modules/@prisma/client/runtime/index.js:31941:13) at Object.handleAndLogRequestError (/Users/luistigre/www/nlw/nlw/aulas/server/node_modules/@prisma/client/runtime/index.js:31913:12) at Object.request (/Users/luistigre/www/nlw/nlw/aulas/server/node_modules/@prisma/client/runtime/index.js:31908:12) at PrismaClient._request (/Users/luistigre/www/nlw/nlw/aulas/server/node_modules/@prisma/client/runtime/index.js:32994:16) at main (/Users/luistigre/www/nlw/nlw/aulas/server/prisma/seed.ts:6:5) { code: 'P2009', clientVersion: '4.9.0', meta: { query_validation_error: 'Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at
Mutation.createOneHabit.data.HabitCreateInput.created_at
: A value is required but not set., Query parsing/validation error at
Mutation.createOneHabit.data.HabitUncheckedCreateInput.created_at
: A value is required but not set.]', query_position: 'Mutation.createOneHabit.data' }, batchRequestIdx: undefined }

An error occurred while executing the seed command: Error: Command failed with exit code 1: tsx prisma/seed.ts

SCHEMA.PRISMA


generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "sqlite"
url      = env("DATABASE_URL")
}

generator erd {
provider = "prisma-erd-generator"
}

model Habit {
id         String   @id @default(uuid())
title      String
created_at DateTime

dayHabits DayHabit[]
weekDays  HabitWeekDays[]

@@map("habits")
}

model HabitWeekDays {
id       String @id @default(uuid())
habit_id String
week_day Int

habit Habit @relation(fields: [habit_id], references: [id])

@@unique([habit_id, week_day])
@@map("habit_week_days")
}

model Day {
id        String     @id @default(uuid())
date      DateTime
dayHabits DayHabit[]

@@unique([date])
@@map("days")
}

model DayHabit {
id       String @id @default(uuid())
day_id   String
habit_id String

day   Day   @relation(fields: [day_id], references: [id])
habit Habit @relation(fields: [habit_id], references: [id])

@@unique([day_id, habit_id])
@@map("day_habits")
}

SEED.TS FILE


import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient()

async function main() {
await prisma.habit.create({
data: {
title: 'Drink 2L of water',
created_at: new Date('2023-01-10T00:00:00:000z')
}
})

}


main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})

PACKAGE.JS

 "prisma": {
"seed": "tsx prisma/seed.ts"
}

I expected the data to be successfully saved into the database.

Answer №1

Consider making this adjustment:

created_at: new Date('2023-01-22T00:00.000z')

Change it to this:

created_at: new Date('2023-01-22')

Alternatively, use this for the current date:

created_at: new Date()

The function new Date('2023-01-22T00:00.000z') produces an invalid date. You can validate this by logging in your browser's console or executing a TS file using node.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The type '{} is not compatible with the type 'IProps'

In my current project, I am utilizing React alongside Formik and TypeScript. The code snippet below demonstrates my usage of the withFormik Higher Order Component (HOC) in my forms: import React from 'react'; // Libraries import........ import { ...

A guide on defining global TypeScript interfaces within Next.js

In the process of developing an ecommerce web application with next.js and typescript, I found myself declaring similar interfaces across various pages and components. Is there a method to create global interfaces that can be utilized by all elements wit ...

Underscore.js is failing to accurately filter out duplicates with _uniq

Currently, I am integrating underscorejs into my angular project to eliminate duplicate objects in an array. However, I have encountered an issue where only two string arrays are being kept at a time in biddingGroup. When someone else places a bid that is ...

Sanity.io's selection of schema field types for efficient and convenient

Hey there, guys! I recently started using Sanity.io and I'm curious whether there's a way to enhance my code efficiency and reuse certain fields across different schemas. I had an idea that goes something like this: cars.ts: export default { ...

Tips and tricks for setting up a functional react select component using Material UI

Having an issue with React Select material UI where the dropdown select is not functioning properly. The popup does not open up as expected. I am looking to display react select in a modal dialog box. import MenuItem from "@mui/material/MenuItem" ...

Issue with login form in IONIC: Form only functions after page is refreshed

Encountering an issue with my Ionic login form where the submit button gets disabled due to invalid form even when it's not, or sometimes displays a console error stating form is invalid along with null inputs. This problem seems to have surfaced afte ...

Enhancing Angular input validators with updates

Working on a project with Angular 6, I have set up an input field using mat-input from the Angular Material framework and assigned it an id for FormGroup validation. However, when I initialize my TypeScript class and update the input value, the validator d ...

Steps to insert a personalized attribute into a TypeScript interface

UPDATED EXPLANATION: I'm fairly new to TypeScript, so please bear with me if this question seems basic. I'm working with an existing library (ngx-logger) that I don't want to or can't modify. My goal is to create a service that generat ...

TS2339: The attribute 'size' is not present on the 'string' data type

Within my Typescript file, I have the following line: return stringToValidate.length <= maxLength; Despite the code executing without issues, an error is displayed: TS2339: Property 'length' does not exist on type 'string'. I am cu ...

Button with circular icon in Ionic 4 placed outside of the toolbar or ion-buttons

Is it possible to create a circular, clear icon-only button without using ion-buttons? I am trying to achieve the same style as an icon-only button within ion-buttons (clear and circular). Here is my current code: <ion-button icon-only shape="round" co ...

The spread operator seems to be malfunctioning whenever I incorporate tailwindcss into my code

Hi there! I hope you're doing well! I've come across a strange issue in Tailwindcss. When I close the scope of a component and try to use props like ...rest, the className doesn't function as expected. Here's an example: import { Butto ...

Checking the formik field with an array of objects through Yup for validation

Here is a snippet of the code I'm working on: https://codesandbox.io/s/busy-bose-4qhoh?file=/src/App.tsx I am currently in the process of creating a form that will accept an array of objects called Criterion, which are of a specific type: export inte ...

Error: Unable to locate the reference for "foo" while utilizing TypeScript in combination with Webpack

My Chrome extension is functioning properly when using JavaScript alone. However, when attempting to incorporate TypeScript with Webpack, I encountered an issue where the function foo could not be found. Uncaught ReferenceError: foo is not defined Here ...

Issue: (SystemJS) Unable to find solutions for all parameters in $WebSocket: ([object Object], [object Object], ?)

Upon running the code snippet below, an error is thrown: Error: (SystemJS) Can't resolve all parameters for $WebSocket: ([object Object], [object Object], ?). app.component.ts import { Component } from '@angular/core'; import {$WebSocket} ...

By utilizing a function provided within the context, React state will consistently have its original value

After passing functions from one component to its parent and then through context, updating the state works fine. However, there is an issue with accessing the data inside these functions. They continue to show as the initial data rather than the updated v ...

Trouble accessing data property within a method in Vue 3 with Typescript

I am facing an issue with accessing my data in a method I have written. I am getting a Typescript error TS2339 which states that the property does not exist in the type. TS2339: Property 'players' does not exist on type '{ onAddPlayers(): vo ...

Unexpected errors are encountered when using ng serve

When I run the ng serve command, unexpected errors are occurring on my system. The errors include: PS C:\Users\SAYED-SADAT\Desktop\data\coding\itsm-frontend\itsm-frontend> ng serveYour global Angular CLI version (13.0 ...

The function signature '(event: ChangeEvent<HTMLInputElement>) => void' does not match the expected type 'ChangeEvent<HTMLInputElement>'

This is my first time using TypeScript to work on a project from the ZTM course, which was initially written in JavaScript. I am facing an issue where I am unable to set a type for the event parameter. The error message I receive states: Type '(event: ...

Using Angular to dynamically modify the names of class members

I am working with an Angular typescript file and I have a constant defined as follows: const baseMaps = { Map: "test 1", Satellite: "test 2" }; Now, I want to set the member names "Map" and "Satellite" dynam ...

Encountering an HTTP parsing failure while sending XML through Angular 5's HttpClient

Struggling to access a local webservice through XML: Take a look at the code below: const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'text/xml', 'Accept': 'text/xml', 'Response- ...