question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to save entities and relatives

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Oxymoron290commented, Sep 5, 2019

A better working example is

val result = entity.Trip.new {
    driver_id = t.driver.id
    truckload_id = t.truckload.id
}.also { newTrip ->
    t.legs.forEach {
        entity.TripLeg.new {
            trip = newTrip
            ordinal = it.order
            phase = it.phase.toString()
            status = it.status.toString()
        }
    }
}
1reaction
Tapaccommented, Sep 4, 2019

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.

            val trip = entity.Trip.new {
                driver_id = trip.driver.id
                truckload_id = trip.truckload.id
           }.also { newTrip ->
                trip.legs.forEach {
                    entity.TripLeg.new {
                        trip = newTrip
                        ordinal = it.order
                        phase = it.phase.toString()
                        status = it.status.toString()
                }
            }
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found