'postgreSQL Models Have Proper ID Attributes, but will not form Relationships
I have a series of models that work with really basic relationships.
There is an Accident model that can have many...
InjuryAccidents
SelfInjuryAccidents
PropertyAccidents
and
CollisionAccidents
These of course, belong to the Accident model, meaning the relationship is a straight forward one-to-many.
The TypeDefs for each look as follows...
Accident
type Accident {
id: ID
createdAt: Date
name: String
date: String
time: String
location: String
# ACCIDENT DATA
accident_report: JSON
has_logo: String
police_report: JSON
before_accident_report: JSON
selfDamage: JSON
weather_and_distractions: JSON
deleted: Boolean
filled: Boolean
# RELATIONSHIPS
driver: Driver
collisionAccidents: [CollisionAccident]
injuryAccidents: [InjuryAccident]
propertyAccidents: [PropertyAccident]
selfInjuryAccidents: [SelfInjuryAccident]
}
Collision Accident
type CollisionAccident {
id: ID
specific_pictures: JSON
contact_info: JSON
collision_report: JSON
extra_info: String
accident: Accident
accidentId: String
injuryAccident: [InjuryAccident]
}
Injury Accident
type InjuryAccident{
id: ID
contact_info: JSON
extra_info: String
injured_areas: JSON
injury_report: JSON
pain_level: String
specific_pictures: JSON
accident: Accident
accidentId: String
collisionAccident: CollisionAccident
collisionId: String
}
Self Injury Accident
type SelfInjuryAccident{
id: ID
animal_report: JSON
injuries: JSON
injury_report: JSON
extra_info: String
specific_pictures: JSON
accidentId: String
accident: Accident
}
I think you get the point, all of the relationships are set up like that. Futhermore, the schema is set up properly as well (I'm only going to show Accident and Property Accidents because they all look very similar)
Accident Model
model Accident {
id String @id @unique @default(uuid())
createdAt DateTime @default(now())
name String
date String
time String
location String
// ACCIDENT DATA
accident_report Json?
has_logo String?
police_report Json?
before_accident_report Json?
selfDamage Json?
weather_and_distractions Json?
deleted Boolean @default(false)
filled Boolean @default(false)
// RELATIONSHIPS
driverId String
driver Driver @relation(fields: [driverId], references: [id])
collisionAccidents CollisionAccident[]
propertyAccidents PropertyAccident[]
injuryAccidents InjuryAccident[]
selfInjuryAccidents SelfInjuryAccident[]
}
Collision Accident Model
odel CollisionAccident {
id String @id @unique @default(uuid())
specific_pictures Json
contact_info Json
extra_info String?
collision_report Json
// RELATIONSHIPS
accidentId String
accident Accident @relation(fields: [accidentId], references: [id])
injuryAccidents InjuryAccident[]
}
Furthermore, after every mutation, I console.log the returned promise and each model (CollisionAccident, InjuryAccident, SelfInjuryAccident, PropertyAccident) had the proper accidentId indicating that the relationship should be there. However, when I query the Accident (which SHOULD own all the other forms of accidents) it will return something like this...
Object {
"data": Object {
"driverUpdateAccident": Object {
"__typename": "Accident",
"accident_report": Object {
"bystander": Object {
"fullname": "",
"phoneNumber": "",
"present": "no",
},
"has_logo": "no",
"police_report": Object {
"filed": "No",
"officer_name": "",
"officer_township": "",
"report_number": "",
},
},
"before_accident_report": Object {
"main_action": "parked-and-occupied",
"setting1": "",
"setting2": "",
"specifics": "",
},
"collisionAccidents": null, // THIS SHOULD NOT BE NULL
"createdAt": "2022-03-25T15:30:44.732Z",
"date": "03/25/2022",
"has_logo": null,
"id": "8ad6cf7d-5233-4a19-8fc5-06bc917f6a3d",
"injuryAccidents": null, // THIS SHOULD NOT BE NULL
"location": "1 Stockton St, San Francisco CA 94108",
"name": "1 Stockton St, San Francisco CA 94108 Accident",
"police_report": null,
"propertyAccidents": null,
"selfDamage": Object {
"damaged": false,
"damages": Object {},
},
"selfInjuryAccidents": null, // THIS SHOULD NOT BE NULL
"time": "11:30:43",
"weather_and_distractions": Object {
"distraction": "No Distraction",
"slippery": "Not Slippery",
"weather": "Sunny",
},
},
},
}
Does anyone have any idea what I could be missing? Whenever I create any of the sub accidents (Property, Collision, Injury, SelfInjury) I put in the accidentId, and to reiterate, they receive it properly since I can get the accidentId returned back from them, so it cannot be their mutation process. Any ideas?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
