How to save entities and relatives
See original GitHub issueI have a trip, and I have legs of that trip. How do I save the two entities while preserving the relationship between the two entities. I have read the documentation several times and either it does a poor job of explaining it or it doesn’t explain it at all.
Here is what I have:
table/Trips.kt
object Trips : IntIdTable("trip") {
val driver_id = integer("driver_id")
val truckload_id = integer("truckload_id")
val insertedAt = datetime("inserted_at")
val updatedAt = datetime("updated_at")
}
entity/Trip.kt
class Trip(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Trip>(Trips)
var driver_id by Trips.driver_id
var truckload_id by Trips.truckload_id
var insertedAt by Trips.insertedAt
var updatedAt by Trips.updatedAt
val tripLegs by TripLeg referrersOn TripLegs.trip
}
table/TripLegs.kt
object TripLegs : IntIdTable("trip_leg") {
val trip = reference("id", Trips)
val ordinal = integer("ordinal")
val phase = varchar("phase", 255)
val status = varchar("status", 255)
val source_location = reference("source_location_id", Locations).nullable()
val destination_location = reference("destination_location_id", Locations).nullable()
val insertedAt = datetime("inserted_at")
val updatedAt = datetime("updated_at")
}
entity/TripLeg.kt
class TripLeg(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<TripLeg>(TripLegs)
var trip by Trip referencedOn TripLegs.trip
var ordinal by TripLegs.ordinal
var phase by TripLegs.phase
var status by TripLegs.status
var source_location by TripLegs.source_location
var destination_location by TripLegs.destination_location
var completions by Duration via TripLegsCompletions
var remainings by Duration via TripLegsRemainings
}
Here is the code I’ve attempted to use and it doesn’t work because entity.Trip.tripLegs is a val
, changing it to a var
causes the referrersOn
to no longer work
At the risk of giving too much info. The value trip
here is of an intermediate type that is not in the data project. .entity.* and .table.* are in it’s own package, separate from the intermediate values.
entity.Trip.new {
driver_id = trip.driver.id
truckload_id = trip.truckload.id
tripLegs = SizedCollection(trip.legs.map {
entity.TripLeg.new {
ordinal = it.order
phase = it.phase.toString()
status = it.status.toString()
//source_location = it.source
//destination_location = it.destination
}
})
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Saving Related Data - EF Core - Microsoft Learn
Information on saving graphs of related entities and managing relationships in Entity Framework Core.
Read more >[Solved]-Save entity with child entity-Entity Framework
How to save an entity with a child entity which already exists in EF core? ... How to delete child entities before parent...
Read more >How to remove entity with ManyToMany relationship in JPA ...
Set<User> users; //... } Now I want to remove a group (let's say it has many members). Problem is that when I call...
Read more >Working with Relations - typeorm - GitBook
Using it, you can bind entities to each other in the database without the need ... post with all ten thousand categories, push...
Read more >Minecraft 1.10 Structure Block Tutorial / Guide - YouTube
Learn How to use Structure Blocks in Minecraft 1.10 with this quick and easy structure block guide! The structure block can be used...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A better working example is
Yep, documentation is very poor and we are going to improve it in the near future.
Answering your question just set
TripLeg .tripLegs
value with created trip instance.