Search code examples
prisma

Can't insert Id to field with a FK relation


I am getting this error in my POST route (full error below):

Unknown argument `countryId`. Did you mean `country`? Available options are marked with ?.

This POST route fails on create:

router.post('/', (req, res) => {
    const {
        modelNominalValue: nominalValue, modelFaceValue: faceValue, modelCommonName: commonName,
        modelObverse: obverse, modelReverse: reverse, modelComposition: composition,
        modelYearStart: yearStart, modelYearEnd: yearEnd, modelDimensionsMilimeters: dimensionsMilimeters,
        modelComments: comments, selectedCountryId: countryId, selectedCurrencyId: currencyId,
        rulerId, periodId, shapeId,
    } = req.body;

    prisma.banknote.create({
        data: {
            nominalValue, faceValue, commonName, obverse, reverse, composition, yearStart, yearEnd,
            dimensionsMilimeters, comments, countryId, currencyId, rulerId, periodId, shapeId,
            createdAt: new Date(), updatedAt: new Date(),
        },
    })
        .then((banknote) => {
            res.status(201).json(banknote);
        })
        .catch((err) => {
            res.status(500).send(err.message);
        });
});

The prisma model is:

model Banknote {
  id                   Int            @id(map: "PK__Banknote__3214EC07EA22598A") @default(autoincrement()) @map("Id")
  nominalValue         Decimal        @map("FaceValue") @db.Decimal(8, 3)
  faceValue            String         @map("PrettyFaceValue") @db.NVarChar(30)
  commonName           String?        @map("CommonName") @db.NVarChar(60)
  obverse              String         @map("Obverse") @db.NVarChar(2000)
  reverse              String         @map("Reverse") @db.NVarChar(2000)
  yearStart            String?        @map("YearStart") @db.NVarChar(255)
  yearEnd              String?        @map("YearEnd") @db.NVarChar(255)
  composition          String?        @map("Composition") @db.NVarChar(255)
  dimensionsMilimeters String?        @map("DimensionsMilimeters") @db.NVarChar(40)
  comments             String?        @map("Comments") @db.NVarChar(2000)
  periodId             Int?           @map("PeriodId")
  countryId            Int?           @map("CountryId")
  currencyId           Int?           @map("CurrencyId")
  createdAt            DateTime       @map("CreatedAt")
  updatedAt            DateTime       @map("UpdatedAt")
  country              Country?       @relation(fields: [countryId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_Banknote_Country")
  currency             Currency?      @relation(fields: [currencyId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_Banknote_Currency")
  period               Period?        @relation(fields: [periodId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "FK_Banknote_Period")
  banknoteMints        BanknoteMint[]
  collections          Collection[]
  images               Image[]
}

Full error:

Invalid `prisma.banknote.create()` invocation:

{
data: {
nominalValue: ".125",
faceValue: "O.125",
commonName: "cool coin",
obverse: "Obverse txt",
reverse: "Reverse txt",
composition: "Good stuff",
yearStart: "2000",
yearEnd: "2002",
dimensionsMilimeters: "12x12",
comments: "Comments",
countryId: 1,
~~~~~~~~~
currencyId: 1,
rulerId: 1,
periodId: 1,
shapeId: 1,
createdAt: new Date("2025-02-05T00:09:16.850Z"),
updatedAt: new Date("2025-02-05T00:09:16.850Z"),
? country?: CountryCreateNestedOneWithoutBanknotesInput,
? currency?: CurrencyCreateNestedOneWithoutBanknotesInput,
? period?: PeriodCreateNestedOneWithoutBanknotesInput,
? banknoteMints?: BanknoteMintCreateNestedManyWithoutBanknoteInput,
? collections?: CollectionCreateNestedManyWithoutBanknoteInput,
? images?: ImageCreateNestedManyWithoutBanknoteInput
}
}

Unknown argument `countryId`. Did you mean `country`? Available options are marked with ?.

How can I make reference to the country by id be accomplished?


Solution

  • Your code looks correct, at least from a quick glance. What is happening here is that Prisma is interpreting this as a normal CreateInput instead of a UncheckedCreateInput.

    Make sure that the Prisma generated types are up-to-date with your Prisma schema (i.e. prisma generate).

    To be sure you could, instead of using an implicit one, explictly define the object containing the data to be inserted in the database as an UncheckedCreateInput (in this case BanknoteUncheckedCreateInput); if there were any errors in your object TypeScript would let you know.

    If this (somehow) still doesn't work, you could always use the "old" way of connecting relations like so

    country: {
      connect: {
        id,
      },
    }