Differences between model and association in inlude during sequencee connection Query

Source: Internet
Author: User
This article mainly introduces the differences between model and association in inlude during sequencee connection query. This article describes the differences in detail. For more information, see the following. Preface

We all know that when we use sequencee to perform connection queries Between Relational Models (tables), we will use model/as to specify the connection query model with an associated relationship, or you can use association to directly specify the connection to query the model relationship. In what scenarios should the two be used?

I. Sample Preparation

Model Definition

First, define the User and Company models:

'Use strict 'const sequencee = require ('sequencee'); // create a sequencee instance const sequencee = new sequencee ('db1', 'root', '123', {logging: console. log}); // defines the User Model var User = sequencee. define ('user', {id: {type: sequencee. BIGINT (11), autoIncrement: true, primaryKey: true, unique: true}, name: {type: sequencee. STRING, comment: 'name'}, sex: {type: sequencee. INTEGER, allowNull: false, defaultValue: 0, comment: 'gender '}, companyId: {type: sequencee. BIGINT (11), field: 'Company _ id', allowNull: false, comment: 'Company '}, isManager: {type: sequencee. BOOLEAN, field: 'Is _ Manager', allowNull: false, defaultValue: false, comment: 'administrator? '}}, {charset: 'utf8', collate: 'utf8 _ general_ci '}); // defines the Company Model var Company = sequencee. define ('company', {id: {type: sequencee. BIGINT (11), autoIncrement: true, primaryKey: true, unique: true}, name: {type: sequencee. STRING, comment: 'Company name' }}, {charset: 'utf8', collate: 'utf8 _ general_ci '}); // defines the User-Company association relationship. belonsto (Company, {foreignKey: 'companyid'}); // sequencee. sync ({force: true }). then () => {// process. exit ();//});

As shown above, the User and Company models are defined and the relationship between User-Company is specified through belonsto.

Insert data

Next, insert some test data based on the newly defined relational model:

Company. create ({name: 'Company '}). then (result) => {return Promise. all ([User. create ({name: 'ho min San ', sex: 1, companyId: result. id, isManager: true}), User. create ({name: 'zhangbin', sex: 1, companyId: result. id})])}. then (result) => {console. log ('done ');}). catch (err) => {console. error (err );});

2. Use model/

If the association between models has been defined during connection query. In the inlude query option, you can use the 'model' attribute to specify the model to be connected to and query. You can also use the 'as' attribute to specify an alias.

For example, you can query a User from the User model and the company information of the User:

Var include = [{model: Company, as: 'company'}]; User. findOne ({include: include }). then (result) => {console. log (result. name + 'is' + result. company. name + 'employee ');}). catch (err) => {console. error (err );});

The query result is as follows:

He MINSAN is an employee of a company.

Iii. Use association

During connection query, if the two models to be connected to the query do not have a connection relationship defined in advance, or you want to use a connection relationship other than the definition. In this case, you can use association to define or redefine the model relationship.

For example, query any Company in the Company model and the Administrator of the Company:

Var include = [{association: Company. hasOne (User, {foreignKey: 'companyid', as: 'manager'}), where: {isManager: true}] Company. findOne ({include: include }). then (result) => {console. log (result. the name + 'administrator is' + result. manager. name );}). catch (err) => {console. error (err );});

Because no model relationship is defined between Company-users, You need to specify the association relationship to be used for connection query in the inlude option.

The query result is as follows:

He MINSAN, the administrator of a Company

Association can be used not only to specify model relationships that have not been previously defined, but also to redefine model relationships. For example, we suppose that there is a 1: N relationship between Company-User through hasexample. This relationship applies to querying all employees in the company. In the above example, we need to check the company Administrator through the relationship. Therefore, we can use association to redefine the model relationship.

For more information about the differences between model and association in inlude during sequencee connection query, see the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.