My data is stored in two tables:
Table 1:
ID | Name | |
---|---|---|
First | row | row |
Second | row | row |
Table 2:
ID | Number | Status |
---|---|---|
First | row | row |
Second | row | row |
I have defined JS models for each table.
This is the model for Table 1:
import Sequelize from "sequelize";
import db from './index.js';
const table1 = db.define("table_1", {
id : {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
name: {
type: Sequelize.STRING(100),
},
email: {
type: Sequelize.STRING(100),
}
}, {
schema: 'example',
tableName: 'table1'
});
export default table1;
This is the model for Table 2:
import Sequelize from "sequelize";
import db from './index.js';
const table2 = db.define("table_2", {
id : {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
number: {
type: Sequelize.STRING(100),
},
status: {
type: Sequelize.STRING(50),
}
}, {
schema: 'example',
tableName: 'table2'
});
export default table2;
I want to perform an Inner Join operation using Sequelize. Instead of resorting to raw SQL, I prefer leveraging Sequelize's capabilities for this task. How can I achieve Inner Join with these tables?
The Inner Join query I envision looks like this:
SELECT
t1.id,
t1.name,
t2.email,
t2.number,
t2.status
FROM
table2 t2
INNER JOIN table1 t1
on t1.id = t2.id
WHERE
t2.email = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcc8d9cfc892c8d9cfc8fcc8d9cfc892dfd3d1">[email protected]</a>';
Once again, my goal is to accomplish this behavior through Sequelize rather than employing raw SQL statements.