Commit Graph

21 Commits

Author SHA1 Message Date
Jinzhu ab703afe97 Merge pull request #1199 from jugglinmike/empty-associations
Return empty slice for associations with 0 results
2016-10-06 21:23:04 +08:00
Jinzhu afaadc3942 Refactor named value support for PolymorphicType 2016-10-06 20:33:48 +08:00
slockij 1413e55339 Add named value for PolymorphicType (to replace scope.TableName) 2016-09-28 22:44:43 +02:00
Mike Pennisi f06d6412de Return empty slice for associations with 0 results
When using `Preload` to include the results of a "has many"
relationship, Gorm previously returned an uninitialized slice for any
such relations that bore zero records. This distinction was most
apparent when the results were marshalled to a JSON representation--a
record with zero related records would be represented with `null`.

For example, consider the following schema:

    id | name
    ---|------
    1  | Lorin
    2  | Sue

    id | p_id | value
    ---|------|-------------------
    1  | 1    | lorin@example.com
    2  | 1    | lorin2@example.com

Querying with:

    db.Preload("Email").Find(&people)

And marshalling the resulting value of `people` to JSON would yield the
following string:

    [
      {
        "name": "Lorin",
        "email": [
          "lorin@example.com",
          "lorin2@example.com"
        ]
      },
      {
        "name": "Sue",
        "email": null
      }
    ]

Beyond being inconsistent, the value `null` in this response differs
semantically from the actual state of the database. The database
actually has zero related records for the second user, so a JSON value
of `[]` is appropriate.

Update the callback that processes "has many" relationships to
communicate empty query results with an empty slice.
2016-09-27 17:09:57 -04:00
Jinzhu caa792644c Include model value when preloading many2many relations to enable row query callbacks 2016-06-17 18:18:46 +08:00
Jinzhu 6ef2a539bd Fix test 2016-06-16 18:18:00 +08:00
Jinzhu 61f50a429a Fix preload has one relations 2016-06-16 17:58:25 +08:00
Jinzhu 277b4e93b6 Merge pull request #1013 from girokon/m2m_preload_issue
fix m2m nested preload
2016-06-16 16:41:34 +08:00
Jinzhu dca5e54f8b Merge pull request #1038 from RichardKnop/bugfix/preload-panic
Fixed an occurrence of a panic while preloading non existent fields.
2016-06-01 23:10:28 +08:00
Björn fcf8dd4e23 Add support for polymorphic preload
Fixes #1051
2016-06-01 13:46:45 +02:00
Richard Knop 8dfcab0f7f Fixed an occurence of a panic while preloading non existent fields. 2016-05-24 00:54:53 +08:00
Igor Noskov 13ac0839c4 fix m2m nested preload 2016-05-12 14:48:08 +06:00
Jinzhu 0fd2fd66d5 Refactor handleHasManyPreload 2016-05-09 22:42:07 +08:00
Dirk-Jan d02c2a37ea Improved preload m2m performance 2016-05-09 14:24:53 +02:00
Jinzhu 846a2d401a Fix scan columns with same name 2016-03-10 17:13:48 +08:00
Jinzhu 3055bad1e8 scope.Fields() return slice of *Field 2016-03-07 14:25:41 +08:00
Jinzhu 5883c70478 Support custom preloading SQL, close #598, #793, #824 2016-02-15 21:29:47 +08:00
Jinzhu 421979cfc2 Order results when preload many2many relations 2016-02-14 17:21:40 +08:00
Jinzhu 896ee534e2 Add indirect method 2016-01-18 12:20:27 +08:00
Jinzhu 07773cc367 Keep refactoring on callbacks 2016-01-17 20:51:11 +08:00
Jinzhu de73d30503 Refactor callbacks 2016-01-17 15:30:42 +08:00