balderdashy / waterline-docs

WARNING: The content in this repo is out of date! See https://github.com/balderdashy/sails-docs for the most up-to-date documentation
452 stars 163 forks source link

Describe how to use associations with existing tables #109

Open KSDaemon opened 8 years ago

KSDaemon commented 8 years ago

Hi! In one-to-one association at the bottom there is a section about using existing tables. But it only covers how to specify a table name and table columns for simple attributes. But how can i specify a foreign key name for association? I mean something like this:

var User = Waterline.Collection.extend({

  identity: 'user',
  connection: 'local-postgresql',
  tableName: 'pb_user'

  attributes: {
    id: {
      type: 'integer',
      primaryKey: true
    },
    firstName: {
      type: 'string',
      columnName: 'pb_owner_first'
    },
    lastName: {
      type: 'string',
      columnName: 'pb_owner_last'
    },

    // Add a reference to Pet
    pet: {
      model: 'pet'
      columnName: : 'pb_pet_id'     // << foreign key existing column name
    }
  }
});

So, from db point, there is a pb_pet_id column, which is foreign key, but from js/waterline view — we'll get a pet object, that is associated with user.

dv336699 commented 8 years ago

this repo is for issues regard waterline-docs, I think you should get an answer if you post your question (which is not an issue) on stackoverflow