Hello everyone, currently upgrading from prisma1 t...
# orm-help
j
Hello everyone, currently upgrading from prisma1 to prisma2, specifically manually reviewing shema after the last step of the upgrade CLI(adding attributes, etc) Cannot quite understand the last step of the upgrade CLI, specifically refactoring of the relation attributes Foreign key's name in the DB -
parentSalesDomain
How model looks before the last step of the upgrade CLI
Copy code
model Calendar {
  id                String          @id @default(cuid()) @db.VarChar(30)
  ...
  parentSalesDomain String?         @db.VarChar(30)
  SalesDomain       SalesDomain?    @relation(fields: [parentSalesDomain], references: [id])
  ...
}
How model looks after the last step of the upgrade CLI
Copy code
model Calendar {
  id                  String          @default(cuid()) @id @db.VarChar(30)
  ...
  parentSalesDomainId String?         @map("parentSalesDomain") @db.VarChar(30)
  parentSalesDomain   SalesDomain?    @relation(fields: [parentSalesDomain], references: [id])
  ...
}
I cannot quite understand: 1. Is it allowed for the
fields
field of the
relation
attribute to reference itself ? 2. What is the role of the
@map
attribute here ? We map it to the underlying foreign key and then don't use this field in the
relation
What I think it should look like. I'm gonna use the new schema to create a new db from it, so I can rename fields(foreign key before - parentSalesDomain, foreign key now - parentSalesDomainId). Is it the right model definition?
Copy code
model Calendar {
  id                  String         @id @default(cuid()) @db.VarChar(30)
  ...
  parentSalesDomainId String         @db.VarChar(30)
  parentSalesDomain   SalesDomain    @relation(fields: [parentSalesDomainId], references: [id])
  ...
}
Thank you. P.S. prisma1 schema
Copy code
type Calendar {
  id: ID! @id
  ...
  parentSalesDomain: SalesDomain!
  ...
}
n
Hey Jack šŸ‘‹ quick notes on your questions:
Is it allowed for theĀ 
fields
Ā field of theĀ 
relation
Ā attribute to reference itself ?
No, this is not allowed, it's a bug in the Upgrade CLI if it produced this.
What is the role of theĀ 
@map
Ā attribute here ? We map it to the underlying foreign key and then don't use this field in theĀ 
relation
Yes, the field is being mapped to the underlying FK šŸ‘ here is what this relation is supposed to look like:
Copy code
model Calendar {
  id                  String          @default(cuid()) @id @db.VarChar(30)
  ...
  parentSalesDomainId String?         @map("parentSalesDomain") @db.VarChar(30)
  parentSalesDomain   SalesDomain?    @relation(fields: [parentSalesDomainId], references: [id])
  ...
}
j
Thank you