balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

sails-mysql@0.12.0 breaks saving associations #6580

Closed wulfsolter closed 5 years ago

wulfsolter commented 8 years ago

This used to work in 0.11.5 but now fails in 0.12.0

Guest.findOne({
  id: id,
}).populate('trips').exec((error, guest) => {

  console.log('add to trip_id ' + trip_id);
  guest.trips.add(trip_id);

  // save
  guest.save(error => {
    console.log('guest.save error: ' + error);
    callback(error, guest);
  });
});

Error message:

guest.save error: [Error (E_UNKNOWN) Encountered an unexpected error]
Error (E_UNKNOWN) :: Encountered an unexpected error
at new WLError (/Users/wulfsolter/Code/sailsproject/node_modules/waterline/lib/waterline/error/WLError.js:25:15)
at /Users/wulfsolter/Code/sailsproject/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:188:17
at /Users/wulfsolter/Code/sailsproject/node_modules/async/lib/async.js:52:16
at /Users/wulfsolter/Code/sailsproject/node_modules/async/lib/async.js:550:17
at /Users/wulfsolter/Code/sailsproject/node_modules/async/lib/async.js:544:17
at _arrayEach (/Users/wulfsolter/Code/sailsproject/node_modules/async/lib/async.js:85:13)
at Immediate.taskComplete (/Users/wulfsolter/Code/sailsproject/node_modules/async/lib/async.js:543:13)
at processImmediate [as _immediateCallback] (timers.js:516:17)

Removing node_modules and npm installing again doesn't fix. Reverting to 0.11.5 on my prod boxes for now.

Versions: Node v5.9.0 Sails 0.12.1

sailsbot commented 8 years ago

@wulfsolter Thanks for posting, we'll take a look as soon as possible. In the meantime, if you haven’t already, please carefully read the issue contribution guidelines and double-check for any missing information above. In particular, please ensure that this issue is about a stability or performance bug with a documented feature; and make sure you’ve included detailed instructions on how to reproduce the bug from a clean install. Finally, don’t forget to include the version of Node.js you tested with, as well as your version of Sails or Waterline, and of any relevant standalone adapters/generators/hooks.

Thank you!

aradnom commented 8 years ago

Ran into what I think might be a related error on 0.12.0 when attempting to populate associations:

2016-03-21T19:55:04.500Z - error: error: Unable to retrieve specified resource:  Error (E_UNKNOWN) :: Encountered an unexpected error
TypeError: Cannot convert undefined or null to object
    at hasOwnProperty (native)
    at utils.object.hasOwnProperty (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/lib/utils.js:28:14)
    at /Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/select.js:115:10
    at arrayEach (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/node_modules/lodash/index.js:1289:13)
    at Function.<anonymous> (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/node_modules/lodash/index.js:3345:13)
    at /Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/select.js:112:7
    at Array.forEach (native)
    at buildSimpleSelect (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/select.js:94:36)
    at new module.exports (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/select.js:42:21)
    at select (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/index.js:283:10)
    at find (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-sequel/sequel/index.js:81:27)
    at Cursor.populateBuffers [as $populateBuffers] (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/lib/adapter.js:619:31)
    at Cursor.run (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-cursor/cursor/cursor.js:45:8)
    at runJoins (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/waterline-cursor/index.js:51:10)
    at __JOIN__ (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/lib/adapter.js:554:9)
    at afterwards (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/lib/connections/spawn.js:84:5)
    at /Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/lib/connections/spawn.js:40:7
    at wrapped (/Users/me/htdocs/bau-api.derp/node_modules/newrelic/lib/transaction/tracer/index.js:155:28)
    at getConnectionCallback (/Users/me/htdocs/bau-api.derp/node_modules/newrelic/lib/instrumentation/mysql.js:168:11)
    at Ping.onOperationComplete [as _callback] (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:99:5)
    at Ping.Sequence.end (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Ping.Sequence.OkPacket (/Users/me/htdocs/bau-api.derp/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:105:8)

Details:  TypeError: Cannot convert undefined or null to object

Isolated this down to populating one-to-many associations. Issue appears to be occurring in waterline-sequel when it attempts to pull the schema for the associated model. In my case I have a Resource model which references a Location model. Here's the Location definition:

attributes: {

    model:          { type: 'string', defaultsTo: 'Location' },
    inactive:       { type: 'boolean', defaultsTo: false },
    name:           { type: 'string', required: true, unique: true },
    description:    { type: 'text' },
    amenities:      { type: 'array' },
    timezone:       { type: 'string', required: true, enum: timezoneEnum, defaultsTo: 'US/Pacfic' },
    city:           { type: 'string' },
    address:        { type: 'json' },
    contact:        { type: 'json' },
    images:         { type: 'array' },
    attributes:     { type: 'json' },
    availability:   { type: 'array' },
    unavailability: { type: 'array' },
    resources:      { collection: 'Resource', via: 'location' }

}

When waterline-sequel goes to pull the schema from the definition, it isn't there for the resources field.

https://github.com/balderdashy/waterline-sequel/blob/master/sequel/select.js#L111

city { type: 'string' }
address { type: 'json' }
contact { type: 'json' }
images { type: 'array' }
attributes { type: 'json' }
availability { type: 'array' }
unavailability { type: 'array' }
resources undefined

Hope that helps. Rolled back to 0.11.5 for the moment.

particlebanana commented 8 years ago

@wulfsolter @aradnom thanks for the heads up, I'll check it out. It should have been backwards compatible with Waterline 0.11 but it looks like there may be some untested issue.

particlebanana commented 8 years ago

Submitted a patch to waterline-sequel and once tested will push out a patch to sails-mysql.

wulfsolter commented 8 years ago

Tried https://github.com/balderdashy/waterline-sequel/pull/84/commits/b07af495df5f6d65d566de2c17147a58e1b00df2 but doesn't resolve the error I found which is, as others have pointed out, in populating of associations as well not just saving.

FWIW, I'm using many-to-many relationship defined as Guest

module.exports = {

  attributes: {

    ...

    trips: {
      collection: 'trip',
      via: 'guests',
    },
  },
};

Trip

module.exports = {

  identity: 'Trip',

  attributes: {

    ...

    guests: {
      collection:     'guest',
      via:            'trips',
      dominant:       true,
    },
  },
};
particlebanana commented 8 years ago

I went ahead and set the latest tag on npm back to 0.11.5 while we work this out.

particlebanana commented 8 years ago

Ok @wulfsolter I submitted https://github.com/balderdashy/waterline-sequel/pull/85 which patches up the m:m test I put together and the one @Bazze had in the waterline-sequel repo. I'm going to do some more manual testing but hopefully this will get this fixed up once and for all.

wulfsolter commented 8 years ago

Thanks @particlebanana works great for me! Issue resolved from my end.

karambeerPrime commented 8 years ago

I am not able to do many to many association in sail version 0.12.3.Is this not resolved yet or I am doing something wrong?

My problem is similar to @wulfsolter. I need Help asap.Also I want custom name for my join table How I can achieve that?

wulfsolter commented 8 years ago

@karambeerPrime everyone "needs" help "asap" ;) If your need is severe and/or urgent, Balderdash are available for consulting on SailsJS.

As you've read this thread, you would have seen that the issue was caused by a temporary regression in sails-mysql and sails was set back to using the older version under it could later be fixed, which it has.

For documentation on how to use associations, please see Sails Concepts and Sails Reference. Everything needed to work with associations in SailsJS is contained in the documentation.