Commit Graph

47 Commits

Author SHA1 Message Date
Geoff Baskwill ec72a4cb6b Call Query callback chain when preloading many2many (#1622)
When using `Preload` on a `many2many` association, the
`Query` callback chain was not being called. This made
it difficult to write a plugin that could reliably get
called regardless of how objects were being queried.

Now `handleManyToManyPreload` will call the `Query`
callback chain for each object that is retrieved by
following the association.

Since the data has already been read by the
`handleManyToManyPreload` method, a new scope setting
called `gorm:skip_queryCallback` is set to `true` before
calling the callbacks. Callbacks can check for the
presence of this setting if they should not be run; the
default `queryCallback` is an example of this case.

Fixes jinzhu/gorm#1621.
2018-02-09 23:22:53 +08:00
Rob Rodriguez e470b44fa8 adding gorm:auto_preload option and related tests 2017-04-27 15:53:39 -07:00
Jinzhu 5730b92954 Fix tests with mssql 2017-03-22 23:50:42 +08:00
Jinzhu eb06255b66 Skip order sql when quering with distinct 2016-12-01 16:16:20 +08: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 ca46038cb4 Fix preload duplicates has many related objects 2016-07-10 21:34:37 +08:00
Richard Knop 24501a3c1a Fixed bug when preload duplicates has many related objects. 2016-07-09 18:51:38 +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
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 a0aa21aec5 Refactor 2016-03-08 22:29:58 +08:00
Jinzhu ec110657da Refactor based on golint 2016-03-07 17:49:55 +08:00
Jinzhu 5883c70478 Support custom preloading SQL, close #598, #793, #824 2016-02-15 21:29:47 +08:00
Jinzhu 86b4b20278 Merge branch 'master' into v1.0_dev 2016-02-14 14:18:35 +08:00
Richard Knop c19412d85a Fixed a nested preload panic bug. 2016-02-10 23:44:41 +08:00
Jinzhu dc435d2225 Merge branch 'master' into v1.0_dev 2016-02-08 22:08:02 +08:00
lscieux 369027a6ae Fix a reflection panic with nested preloads 2016-02-04 21:28:17 +01:00
Jinzhu 896ee534e2 Add indirect method 2016-01-18 12:20:27 +08:00
Richard Knop 50fe671928 Fixed an issue with reflect.Value.FieldByName causing panic under certain circumstances. 2016-01-17 23:18:16 +08:00
Jinzhu 8d716be896 Fix some go vet/lint reports 2016-01-15 21:16:48 +08:00
Jinzhu 41620f3d6c Fix assign relations to duplicated pointer fields 2016-01-15 11:04:49 +08:00
Jinzhu d9229c5a7b Extract method Scan from rows 2016-01-13 16:53:11 +08:00
Jinzhu bfd421f999 Add TestNestedManyToManyPreload3 2016-01-13 16:00:43 +08:00
Jinzhu 3a9367612e Port changes made in PR #652 2016-01-05 08:34:17 +08:00
Jinzhu e541ca5cdf Fix DropTableIfExists with string 2015-12-16 21:00:56 +08:00
Richard Knop 39e6f58111 There was still an issue with preloading and pointers.
This was actually broken with the refactoring. I added a test to catch
the other place the error was occuring. See the new
TestManyToManyPreloadForNestedPointer test in preload_test.go.
2015-12-16 17:30:58 +08:00
Jinzhu 861c477a33 Test Preload won't panic when nothing found 2015-12-16 10:35:22 +08:00
Jinzhu 15169e635a Add two more nested many2many preload tests 2015-12-16 10:25:07 +08:00
Jinzhu 2f9abdcf02 Fix preload many2many problem when no search results, #688 2015-11-16 12:20:00 +08:00
Jinzhu 88188b6161 Respect Where when create index 2015-10-01 06:53:15 +08:00
Nikola Kovacs a8d7a5fa34 add test for unaddressable value in preload 2015-09-24 14:08:36 +02:00
Jinzhu 9982134955 Fix preload many2many with multiple primary keys 2015-08-18 09:19:50 +08:00
Jinzhu 7ec2b87312 Add more preloading many2many tests 2015-08-18 07:47:00 +08:00
Jinzhu f8e2f04562 Some Tweaks for Preload Many2Many, Add tests with inline conditions 2015-08-17 23:09:07 +08:00
Jinzhu 27511118fe Update Preload Many2Many tests 2015-08-17 22:19:10 +08:00
kimiby f35dee5531 preload_m2m fix and test 2015-08-16 12:25:25 +03:00
Jinzhu 7693c093a9 Refactor Preload 2015-04-22 15:36:10 +08:00
Jinzhu 7ef8b06cb4 Fix tests with mysql 2015-04-22 14:28:50 +08:00
bom_d_van 9e9367e815 refactor preload and its tests 2015-04-21 16:52:18 +08:00
bom_d_van 6d58dc9f4e support nested preloading 2015-04-21 16:52:18 +08:00
Jinzhu ad251b9deb Create with Select 2015-03-12 17:47:31 +08:00
Jinzhu b3b87d9c45 Fix Preload belongs to relations has same records 2015-02-12 13:50:16 +08:00
Jinzhu 24e0de116a Add inline condition support for Preload 2015-02-11 19:08:42 +08:00
Jinzhu aac52fdcf8 Fix Preload with slice of pointer 2015-02-11 17:58:19 +08:00
Jinzhu f97e2c088e Make Preload works with has_one, has_many, belongs_to relations 2015-02-11 17:17:27 +08:00
Jinzhu dd635a17c2 Preload belongs_to, has_one, has_many relations 2015-02-11 13:43:53 +08:00