简体   繁体   中英

Sails.js associations: “Many” side of one-to-many associations not saving to MongoDB or Postgres (vs. disk db)

This works with sails-disk adapter but not with sails-mongo or sails-postgresql.

I have 2 models, Feed and Event, where Feed can belong to many Events.

When I create or save a new Feed record with an association to one or more Events (via "posts" property), the “posts” property is not saved. However, when I create or save a new Event with an association to one Feed (via the "posted" property), the "posted" property is saved properly. The problem: I can't query a Feed and its Events (using Sails's .populate ) from the Feed side, only from the Event side.

I can confirm that the data being passed to Feed.create contains a "posts" property and then the response from the callback does not.

sails.log.debug('Before Feed.create', data);

  Before Feed.create { id: 64988,
  username: 'anevening',
  displayName: 'An Evening',
  coverImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  profileImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  description: undefined,
  links: [],
  source: 
   { domain: 'dola.com',
     id: 64988,
     url: 'http://www.dola.com/artists/an-evening' },
  posts: [ 3055349, 3062549, 2866105, 2866107 ] }

Feed.create(data).exec(sails.log.debug);

  { username: 'anevening',
  displayName: 'An Evening',
  coverImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  profileImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
  description: null,
  links: [],
  source: 
   { domain: 'dola.com',
     id: 64988,
     url: 'http://www.dola.com/artists/an-evening' },
  createdAt: Sat Nov 22 2014 14:53:21 GMT-0800 (PST),
  updatedAt: Sat Nov 22 2014 14:53:21 GMT-0800 (PST),
  id: '64988' }

Here's what my models look like:

Feed.js:

module.exports = {

  attributes: {

    id: {
      type: 'integer',
      unique: true,
      primaryKey: true
    },
    ...
    posts: {
       collection: 'event',
       via: 'posted'
    }
    ...

  }
};

Event.js:

module.exports = {

  attributes: {

    id: {
      type: 'integer',
      unique: true,
      primaryKey: true
    },
    ...
    posted: {
      model: 'feed'
    }
    ...

  }

};

Lastly, here's what my config/models.js and config/connections.js look like:

connections: {

 'default': 'mongo',

    localDiskDb: {
      adapter: 'sails-disk'
    },

    mongo: {
      adapter: 'sails-mongo',
      url: process.env.MONGO_HOST',
      schema: true
    },

    postgres: {
      adapter: 'sails-postgresql',
      url: process.env.POSTGRES_HOST,
      schema: true,
      ssl: true
    }

  },

  models: {
    connection: 'mongo',
    migrate: 'alter'
  }

The .create method doesn't return the populated data in it's callback. If you query the Feed model and populate the posts association you should see the data.

Feed.find()
.populate('posts')
.exec(console.log);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM