简体   繁体   中英

sailsjs one-way-association implementation

i follow this link and trying to show the data

http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-way-association

but i got the error "Unknown column 'shop.building_detail' in 'field list' "

Is it the sails bug or something i did wrong ?

there are my database design and my code in below

database design

Shop model:

module.exports = {
  autoPK:false,
  attributes : {
  shop_id : {
    primaryKey : true,
    type : 'int',
    unique: true,
    columnName : 'shop_id'
  },
  shop_floor : {
    type : 'string'
  },
  shop_room : {
    type : 'string'
  },
  shop_build : {
    type : 'int',
    foreignKey:'true'
  },
  building_detail : {
    model : 'building'
  }
 }
 };

Building model:

module.exports = {
  autoPK:false,
  attributes : {
    build_id : {
      primaryKey : true,
      type : 'int',
      unique: true,
      columnName : 'build_id'
    },
    build_name : {
       type : 'string'
    },
    build_address : {
       type : 'string'
    },
    build_latitude : {
       type : 'float'
    },
    build_longitude : {
       type : 'float'
    },
    build_visit_count : {
       type : 'int'
    },
    build_status : {
       type : 'string'
    },
    build_status_last_update_time : {
       type : 'time'
    }
  }
  };

With your database design, your Shop model could look like this:

Shop.js

module.exports = {
  autoPK:false,
  attributes : {
    shop_id : {
      primaryKey : true,
      type : 'int',
      unique: true,
      columnName : 'shop_id'
    },
    shop_floor : {
      type : 'string'
    },
    shop_room : {
      type : 'string'
    },
    shop_build : {
      model: 'Building'
    }
  }
};

Sails.js automatically associates shop_build with the primary key of the Building model.

When you create a shop, just specify the building id as the value for shop_build :

Shop.create({
  shop_floor: "Some floor",
  shop_room: "Some room",
  shop_build: 8 // <-- building with build_id 8
})
.exec(function(err, shop) { });

And when you perform shop queries, you can populate the building details:

Shop.find()
.populate('shop_build')
.exec(function(err, shops) {
  // Example result:
  // [{
  //   shop_id: 1,
  //   shop_floor: 'Some floor',
  //   shop_room: 'Some room',
  //   shop_build: {
  //     build_id: 8,
  //     build_name: 'Some building',
  //     ...
  //   }
  // }]
});

See Waterline documentation on associations for more details.

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