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.