Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested eager loading along OptionalParent relationship crashes #208

Closed
tarag opened this issue Mar 11, 2020 · 0 comments
Closed

Nested eager loading along OptionalParent relationship crashes #208

tarag opened this issue Mar 11, 2020 · 0 comments
Labels
bug Something isn't working
Projects

Comments

@tarag
Copy link
Contributor

tarag commented Mar 11, 2020

When performing several nested eager loading of parents along the same OptionalParent relationship, it crashes in ThroughOptionalParentEagerLoader run() (fatal error: Unexpectedly found nil while unwrapping an Optional value: file fluent-kit/Sources/FluentKit/Properties/OptionalParent.swift, line 153) when reaching the last parent that has no valid parent and attempting again to eager load this non existing parent:

public final class ZoneRecord : Model, Content {
…    
    @OptionalParent(key: "parent_id")
    public var parent : ZoneRecord?
…
}

return ZoneRecord.query(on: req.db)
    .filter(\.$id == id)
    .with(\.$parent) {
        $0.with(\.$parent) {
            $0.with(\.$parent)
        }
    }
    .first()

Example:

ZoneRecord id = 1, parent_id=nil
ZoneRecord id = 2, parent_id=1
ZoneRecord id = 3, parent_id=2

If I perform the query for a ZoneRecord that has a valid parent and grand parent it works as expected (e.g. id = 3 above), but for one that has only one parent and no grand-parent it will crash (E.g id = 2 above).

Fluent-Kit: 1.0.0-rc.1.8
fluent-postgres-driver 2.0.0-rc.1

@tanner0101 tanner0101 added the bug Something isn't working label Mar 12, 2020
@tanner0101 tanner0101 added this to To Do in Vapor 4 via automation Mar 12, 2020
Vapor 4 automation moved this from To Do to Done May 8, 2020
tanner0101 added a commit that referenced this issue May 29, 2020
* Uniform field paths (#268)

* unify field path structure for all drivers

* cleanup files

* rm dead code

* nested group option

* protocol cleanup + timestamp updates

* updates

* add Database.inTransaction (#192)

* optional field updates

* transaction + optional updates

* add batch delete, fixes #114

* add constraint names, fixes #118

* support arbitrary join ordering, fixes #128

* add nil update tests, fixes #149

* add EnumBuilder.read(), fixes #194

* support @OptionalParent eager load, fixes #208

* test alias nesting, fixes #223

* parent optional

* Fix dummy db placeholder (#200)

* Add tests for placeholder for dummy database

* Fix placeholder position

Placeholder position should start at 1, not 2.
No need to add 1 to position.

* Rename variable to result

* crud updates

* clear method and middleware in batch creation (#220)

Co-authored-by: Tanner <tannernelson@gmail.com>

* merge conflicts

* todo

* batch delete middleware

* Add entire models to the test database output. (#265)

Co-authored-by: Tanner <tannernelson@gmail.com>

* readble coding errors, fixes #231

* add decode test

* add alter table constraints, fixes #235

* skip constraint test sqlite

* fix @enum and @OptionalEnum decode, fixes #249

* add fluent sql sort overloads, fixes #266

* optional field batch create test, fixes #272

* add join partial select, fixes #274

* remove XCTUnwrap usages, fixes #278

* checkout postgres fluent gm branch

* fix unit tests

* setup dual dbs

* retest

* Make siblings relation properties public (#275)

* add filter to partial select, fixes #274

* fix @OptionalParent.get when nil

* add test for set to nil, fixes #284

* doc blocks

* updat license year

* use sqlite gm branch

* package update

* mysql updates

* fix workflows

* fluent branch

* update mongo

* rm nested

Co-authored-by: Jean-Charles Campagne <jc.campagne@gmail.com>
Co-authored-by: Mads Odgaard <mads@madsodgaard.com>
Co-authored-by: Mathew Polzin <matt.polzin@gmail.com>
Co-authored-by: Florian Friedrich <ffried@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Vapor 4
  
Done
Development

No branches or pull requests

2 participants