https://jobinandjismi.atlassian.net/browse/GED-542 Sequelize eager loading is a way to fetch data from multiple tables that have relations between each other. When you use the eager loading technique, the generated SQL query will have one or more JOIN clauses. Suppose you have two tables in your database named Users and Invoices as follows # Users | id… Continue reading Sequelize eager loading explained with examples
Tag: sequalize
How to fix Unable to resolve sequelize package error
When running any sequelize-cli command from the Terminal, you may encounter an error saying Unable to resolve sequelize package. Here’s an example of the error: $ npx sequelize-cli init Unable to resolve sequelize package in … This error happens because sequelize-cli requires the sequelize package to run. To resolve this error, you need to install sequelize package locally in your project with the following… Continue reading How to fix Unable to resolve sequelize package error
How to insert new rows using Sequelize
To insert new rows into your SQL table, you need to first create a Sequelize Model object. In Sequelize, a Model is an object that represents your SQL table, giving information to Sequelize about the name, the columns, and their data types. From the Model, you can then call the create() method, which performs an INSERT SQL statement to your table. Let’s see a… Continue reading How to insert new rows using Sequelize
Sequelize data types list
Sequelize provides support for most data types that you can find in SQL-based databases. When defining types, you can use both Sequelize class and DataTypes class interchangeably as shown below: const User = sequelize.define( “User”, { firstName: DataTypes.STRING, lastName: Sequelize.STRING, }, { timestamps: false, } ); Some of the data types supported by Sequelize are… Continue reading Sequelize data types list
Sequelize belongsTo() association method
The Sequelize belongsTo() method allows you to create a One-To-One (1:1) relationship between two Sequelize models. The method is called from a source model and pointed towards the model passed as its argument. For example, suppose we have a customer and address models defined like this: const customer = sequelize.define( “customer”, { firstName: Sequelize.STRING },… Continue reading Sequelize belongsTo() association method
Working with ENUM type values in Sequelize
Sequelize provides the ENUM data type that we can use to create and interact with SQL tables that has ENUM as its column type. To use the ENUM type, use either Sequelize.ENUM or DataTypes.ENUM as the data type for a model attribute. In the example below, the User model has the status attribute that’s an ENUM type: const User = sequelize.define( “User”, { firstName: Sequelize.STRING,… Continue reading Working with ENUM type values in Sequelize
How to format the date for date types values using Sequelize
Customizing Sequelize date format with Sequelize.fn() Thesequelize.fn()method is used to call a native database function to modify the way your query worksFor example, you can call the UPPER() function of MySQL and apply it to a column that you have as follows: The sequelize.fn() method is used to call a native database function to modify the way… Continue reading How to format the date for date types values using Sequelize
How to execute/ use a raw query with Sequelize
When using Sequelize to manipulate your database from a JavaScript application, there might be cases where you want to just execute a raw SQL query instead of using Sequelize Model methods. By default the function will return two arguments – a results array, and an object containing metadata (such as amount of affected rows, etc).… Continue reading How to execute/ use a raw query with Sequelize
How to add primary key constraint in Sequelize
Sequelize provides the primaryKey option that you can add to your Sequelize model attributes. The primaryKey option allows you to assign the PRIMARY KEY constraint to your model columns. When you sync() the above Sequelize model, the user_id column will be created as the PRIMARY KEY column of the Users table in your database. You… Continue reading How to add primary key constraint in Sequelize