How to get data from other table using a column data from a different table?

只愿长相守 提交于 2019-12-20 07:22:15

问题


I have two tables:

table1

id | email
1 | email1
2 | email2

table2

userid | username
2      | user1
3      | user2

Now, using sails.js associations, I want to do this:

I have a username = user1. Using this, first I need the userid of this user1, then using this userid, I want to access the email from table1.

I want to do this using models, controllers and associations only. Is it possible?

EDIT:

The tables I have provided above are sample tables for my original tables. Here I'm posting my actual models for actual tables:

Corporate_info.js (For table1. Actual table name: corporate_info)

module.exports = {
  tableName: 'corporate_info',
  autoCreatedAt: false,
  autoUpdatedAt: false,
  connection: 'mysqlAdapter',
  attributes: {
    id: {
      type: 'integer',
      required: true,
      autoIncrement: true,
      primaryKey: true,
      size: 11
    },
    fname: {
      type: 'string',
      required: true,
      size: 100
    },
    lname: {
      type: 'string',
      required: true,
      size: 100
    },
    country_code: {
      type: 'string',
      required: true,
      size: 45
    },
    mobile: {
      type: 'string',
      required: true,
      unique: true,
      size: 100
    },
    email: {
      type: 'string',
      required: true,
      size: 100
    },
    address: {
      type: 'string',
      required: true,
      size: 100
    },
    userid: {
      type: 'integer',
      required: false,
      size: 11
    },
    imei_number: {
      type: 'string',
      required: false
    },
    owner: {
      model: 'Rc_users',
      unique: true
    }

  }
};

Rc_users.js (for table2. Actual table name: rc_users)

module.exports = {
  tableName: 'rc_users',
  autoCreatedAt: false,
  autoUpdatedAt: false,
  connection: 'mysqlAdapter',
  attributes: {
    id: {
      type: 'integer',
      required: true,
      autoIncrement: true,
      primaryKey: true,
      size: 11
    },
    country_code: {
      type: 'string',
      required: true,
      size: 45
    },
    username: {
      type: 'string',
      required: true,
      unique: true,
      index: true,
      size: 255
    },
    password: {
      type: 'string',
      required: true,
      size: 40
    },
    code: {
      type: 'string',
      required: true,
      index: true,
      size: 40
    },
    active: {
      type: 'string',
      required: true,
      size: 3
    },
    last_login: {
      type: 'integer',
      required: true,
      index: true,
      size: 11
    },
    last_session: {
      type: 'string',
      required: true,
      index: true,
      size: 40
    },
    blocked: {
      type: 'string',
      required: true,
      size: 3
    },
    tries: {
      type: 'integer',
      required: true,
      size: 2
    },
    last_try: {
      type: 'integer',
      required: true,
      index: true,
      size: 11
    },
    email: {
      type: 'string',
      required: true,
      size: 255
    },
    mask_id: {
      type: 'integer',
      required: true,
      size: 6
    },
    group_id: {
      type: 'integer',
      required: true,
      size: 6
    },
    activation_time: {
      type: 'integer',
      required: true,
      index: true,
      size: 11
    },
    last_action: {
      type: 'integer',
      required: true,
      index: true,
      size: 11
    },
    firstname: {
      type: 'string',
      required: false,
      size: 40
    },
    lastname: {
      type: 'string',
      required: false,
      size: 40
    },
    companyname: {
      type: 'string',
      required: false,
      size: 100
    },
    reg_type: {
      type: 'integer',
      required: true,
      size: 11
    },
    rc_web_userid: {
      type: 'string',
      required: false,
      size: 100
    },
    admin_id: {
      type: 'integer',
      required: false,
      size: 11
    },
    device_token: {
      type: 'string',
      required: false,
      size: 500
    },
    device_type: {
      type: 'string',
      required: false,
      size: 45
    },
    userMobile: {
      collection: 'corporate_info',
      via: 'owner'
    }
  }
};

回答1:


You should create model like this

I assumed that you want to do one-by-one association. So this is it.

api/models/Email.js

attributes: {
    email : {
        type: 'email',
        unique: true
    },
    owner : {
        model:'user',  //here put your model name 
        unique: true   //put unique here because it's one by one association
   }
}

api/models/User.js

attributes: {
   username : {
     type: 'string',
     unique: true
   },
   userEmail : {
      collection:'email', //here is also model name
      via: 'owner'
   }
}

Then

Get user from email

Email.find().populate('owner')

Get email from user

User.find().populate('userEmail')

Now you can access to your data both from your two model.

Try to print two command above you will see that your data contain the data from related table.

Email.find().populate('owner').exec(function(err, records) {
    res.json(records)
});

This is my response.

[
    {
        "owner": {
            "username": "test",
            "id": 1,
            "createdAt": "2016-11-23T13:45:06.000Z",
            "updatedAt": "2016-11-23T13:45:06.000Z"
        },
        "email": "test@test.com",
        "id": 1,
        "createdAt": "2016-11-23T13:45:06.000Z",
        "updatedAt": "2016-11-23T13:45:06.000Z"
    }
]

More information : http://sailsjs.com/documentation/concepts/models-and-orm/associations/one-to-one



来源:https://stackoverflow.com/questions/40764940/how-to-get-data-from-other-table-using-a-column-data-from-a-different-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!