hi please, I am trying to create a 1-to-1 relation...
# orm-help
n
hi please, I am trying to create a 1-to-1 relationship between two tables Traveler and Passport, and this is my schema.
Copy code
model Passport {
  passportId        Int      @id @default(autoincrement())
  passportNo        String
  nationality       String
  issuingCountry    String
  expiryDate        DateTime
  gender            String
  firstName         String
  lastName          String
  otherNames        String
  dateOfBirth       String
  isTravelCompanion Boolean?
  userId            Int? // relation scalar field  (used in the `@relation` attribute below)
  user              User?    @relation(fields: [userId], references: [id])

  //Passport relation
  traveller Traveller?

  @@map(name: "passport")
}
Copy code
model Traveller {
  travellerId   Int          @id @default(autoincrement())
  title         String
  firstName     String
  lastName      String
  countryCode   String
  phoneNo       String
  address       String
  state         String
  country       String
  bookingRef    String
  travellerType String?      @default("adult")
  price         String
  currency      String
  isUser        Boolean      @default(false)
  userId        Int? // relation scalar field (used in the '@relation()` attribute below)
  user          User?        @relation(fields: [userId], references: [id])
  flightBooking FlighBooking @relation(fields: [bookingRef], references: [bookingRef])

  // passport relations
  passportId Int
  passport   Passport @relation(fields: [passportId], references: [passportId])

  @@map(name: "traveller")
}
but it shows this error when i run migrate,
Error:
:warning: We found changes that cannot be executed:
`• Step 1 Added the required column
passportId
to the
traveller
table without a default value. There are 15 rows in this table, it is not possible to execute this step.` can someone please tell where I went wrong ?
a
If the column is required, it must have a default value if there are already rows in that table. If there’s no default value, all the existing rows would violate the constraint, and that’s not allowed. So you would have to either set the default value or make the field nullable. 🙂
p
Sorry I see you have user table, then to update my comment. You have duplicate fields in the other tables that should be in User. 😀
n
Hi @Aleksandra Jovanović, is there a way to migrate and wipe the data ?
p
Delete your migration files and redo a migration and it will wipe the data when creating the new tables.
Make sure you have backups
n
Okay , so it won't show the error again of creating default values ? @Paul
p
I think if you simplify your schema, these issues may disappear.