Categories
Mastering Development

Error: Cannot read property ‘_id’ of undefined shows up every time i want to see my own prepered errors

I have a problem and I’ve spent so much time trying to figure it out that I just have to ask for help. I have a school project. My goal is to build a simple website using node.js and sequelize. Page is based on 3 tables in a MySQL database: User, Game and GameDone. GameDone […]

Categories
Mastering Development

Sequelize bulkCreate returns generated IDs or not with MySQL?

When I read the Sequelize docs it makes me think the generated IDs cannot be returned from a call to bulkCreate. Even the returning option parameter says it’s a Postgres-only feature. This is also referenced in this Sequelize issue. But I ran the code below, with Sequelize v5.21.3 and MySQL 5.6.49, and it seems to […]

Categories
Mastering Development

How to relate in sequelize

I am trying to relate my Users model with the Posts model, what I want to do is that the user when creating a post saves the post id in the ‘posteds’ field of the user model, but I can’t do it Post Table: ‘posteds’ shouldn’t be there, it should be in the users table […]

Categories
Mastering Development

Sequelize Error on DataTypes.ARRAY(DataTypes.STRING)

I am new to NodeJs development I am using NodeJs with mysql and Sequelize to create a Batch model with these properties. const Batch = sequelize.define( "Batch", { title: { type: DataTypes.STRING, allowNull: false }, studentIds: { type: DataTypes.STRING }, teacherId: { type: DataTypes.STRING, allowNull: true } }, { timestamps: false } ); On async […]

Categories
Mastering Development

Insert into one-to-many relationship table using sequelize with existing foreign key

I am having the following models: Sports.init({ name: { type: DataTypes.STRING, unique: true, allowNull: false } }, {sequelize, tableName: ‘sports’}); Leagues.init({ name: { type: DataTypes.STRING, unique: true, allowNull: false } }, {sequelize, tableName: ‘leagues’}); The association between them is the following. Sports.Leagues = Sports.hasMany(Leagues, {foreignKey: {name: "sports_id", allowNull: false}, sourceKey: "id"}); Leagues.Sports = Leagues.belongsTo(Sports, {foreignKey: […]

Categories
Mastering Development

Get data from main table by querying sub table and main table

I’ve got table product and sub table series in one too many relation const Product = sequelize.define(‘product’, { id: {type: Sequelize.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true}, order: Sequelize.STRING }) const Series = sequelize.define(‘series’, { id: {type: Sequelize.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true}, name: Sequelize.STRING, }) Relation Product.belongsTo(Series); Series.hasMany(Product); I want to get all […]

Categories
Mastering Development

Model ‘feedback’ is not associated to Model ‘user’ – Sequelize (Node JS)

I am working on an application where I want to retrieve data from two tables. USERS and FEEDBACKS Every user has multiple feedbacks, hence 1 to many relations between USERS and FEEDBACKS. I am using Sequelize for modeling. I have created two Models, with the following format. The ratedBy in FEEDBACKS points to the id […]

Categories
Mastering Development

SEQUELIZE: belongsTo called with something that’s not a subclass of Sequelize.Model

I’m newbie with sequelize and I’m trying to assiacite two tables. Users and Projects. I have defined a model for each one called UserModel and ProjectModel and when I try to associate projects with user I’ve got this error: belongsTo called with something that’s not a subclass of Sequelize.Model These are the files where I […]

Categories
Mastering Development

Cannot add foreign key constraints

I have checked this answer and made sure with both scenario are correct. But still having issue creating constraints: Model: user.js ‘use strict’; module.exports = (sequelize, DataTypes) => { const User = sequelize.define(‘User’, { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: DataTypes.INTEGER }, username: { type: DataTypes.STRING, unique: true, allowNull: false }, password: […]

Categories
Mastering Development

how to use transactions in sequelize?

I have written two classes and migrations file for mysql database using sequelize. how can I save the user and his children using transaction in node.js? I want to create, a class where I can encapsulate all the database communications in one file and then call it from my CRUD methods. user class ‘use strict’; […]