Search code examples
mysqltypescriptnestjsprisma

Prisma One-to-One update Parent or update Parent and Child


I have a Parent Child (One-To-One) Relationship like this:

model Account {
  id Int @id @default(autoincrement())

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  billingAddress Address?
  name           String

  @@map("Accounts")
}

model Address {
  id         Int      @id @default(autoincrement())
  city       String?
  country    String?
  postalCode Int?
  state      String?
  street     String?
  accountId  Int      @unique
  account    Account  @relation(fields: [accountId], references: [id])
}

I want to be able to Update the Parent Record without the need of updating also the Child Record. Furthermore, it would be great, if I can update the Parent Record and the Child Record at the same time. Right now I am getting an Error when only trying to send the Data for the Parent Record.

Here are my DTOs to Create and Edit the Entities:

Create / Edit Account:

export class CreateAccountDto {

    @IsString()
    @IsOptional()
    name: string;

    @IsOptional()
    billingAddress?: CreateAddressDto;

}

Create / Edit Addresss:

export class EditAddressDto {

    @IsString()
    @IsOptional()
    city?: string;

    @IsString()
    @IsOptional()
    country?: string;

    @IsNumber()
    @IsOptional()
    postalCode?: number;

    @IsString()
    @IsOptional()
    state?: string;

    @IsString()
    @IsOptional()
    street?: string;

    @IsInt()
    @IsOptional()
    accountId: number;

}

I'm creating and editing the Account like this:

async editAccount(accountId: number, dto: EditAccountDto) {
    let account;

    console.log({dto})

    account = await this.prisma.account.update({
        where: {
            id: accountId
        },
        data: {
            ...dto,
            billingAddress: {
                update: {
                    ...dto.billingAddress
                }
            }
        },
        include: {
            billingAddress: true
        }
    });

    console.log(account)

    return account;
}

When i try to Edit the Account with the following Data

{
    "name": "Test Account Create2",
    "billingAddress": {
        "id": 2,
        "city": "Dortmund",
        "state": "NRW",
        "postalCode": 44442,
        "country": "Germany",
        "street": "Benninghofer Heide 63",
        "accountId": 10000001
    }
}

i am getting the following Error:

Unknown arg `accountId` in data.billingAddress.update.accountId for type AddressUncheckedUpdateWithoutAccountInput. Did you mean `country`? Available args:
type AddressUncheckedUpdateWithoutAccountInput {
  id?: Int | IntFieldUpdateOperationsInput
  city?: String | NullableStringFieldUpdateOperationsInput | Null
  country?: String | NullableStringFieldUpdateOperationsInput | Null
  latitude?: Decimal | NullableDecimalFieldUpdateOperationsInput | Null
  longitude?: Decimal | NullableDecimalFieldUpdateOperationsInput | Null
  postalCode?: Int | NullableIntFieldUpdateOperationsInput | Null
  state?: String | NullableStringFieldUpdateOperationsInput | Null
  street?: String | NullableStringFieldUpdateOperationsInput | Null
}


    at Document.validate (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:29297:20)
    at serializationFn (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:31876:19)
    at runInChildSpan (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:25100:12)
    at PrismaService._executeRequest (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:31883:31)
    at consumer (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:31810:23)
    at C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:31815:51
    at AsyncResource.runInAsyncScope (node:async_hooks:199:9)
    at C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:31815:29
    at runInChildSpan (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:25100:12)
    at PrismaService._request (C:\Users\Simon\IdeaProjects\crm-tool\crm-backend\node_modules\@prisma\client\runtime\index.js:31812:22)

Solution

  • The error says, you are not allowed to specify accountId when you are updating the address in this way. You can just remove it from your DTO and everything should be fine.