Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize with MYSQL: Raw query returns a "duplicate" result

I have this method that performs a raw query:

 Friendship.getFriends= async (userId)=>{

      const result = await sequelize.query(`select id,email from users where 
      users.id in(SELECT friendId FROM friendships where friendships.userId = 
     ${userId})`);

      return result;
   };

The result seems to contain the same exact data, but twice:

[ [ TextRow { id: 6, email: '[email protected]' },
TextRow { id: 1, email: '[email protected]' } ],
[ TextRow { id: 6, email: '[email protected]' },
TextRow { id: 1, email: '[email protected]' } ] ]

Only two records should actually be found by this query(id's 1 and 6), yet it returns an array with the same records twice.

Can somebody explain me what's going on here?

Edit: the models:

module.exports = (sequelize, DataTypes) => {
   const User = sequelize.define('User', {
     email: { type: DataTypes.STRING, unique: true },    
     password: DataTypes.STRING,
     isActive:{type:DataTypes.BOOLEAN,defaultValue:true}
  });

module.exports = (sequelize, DataTypes) => {
   const Friendship = sequelize.define('Friendship', {
     userId: DataTypes.INTEGER,    
     friendId: DataTypes.INTEGER,      
  });
like image 444
i.brod Avatar asked Nov 24 '18 17:11

i.brod


People also ask

How do I stop a repetition in MySQL?

The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique.

Does Sequelize work with MySQL?

Sequelize is a Node. js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more. An Object Relational Mapper performs functions like handling database records by representing the data as objects.

Does Sequelize query return a promise?

Closing the connection​ Sequelize will keep the connection open by default, and use the same connection for all queries. If you need to close the connection, call sequelize.close() (which is asynchronous and returns a Promise).

What is required false in Sequelize?

sequelize fails to load a model if a nested include has a where clause that returns no models.. to ensure it doesnt fail completely, you can set a required: false clause along with the where clause.. this makes sequelize return a blank array instead of failing the load completely.. Save this answer.


2 Answers

Try to set query type to SELECT in the second argument.

sequelize.query(queryString, {type: sequelize.QueryTypes.SELECT})
like image 160
DmytroK Avatar answered Nov 17 '22 01:11

DmytroK


I am not sure but try below code.

const result = await sequelize.query("select id,email from users where 
      users.id in(SELECT friendId FROM friendships where friendships.userId = 
     ${userId})", {type: sequelize.QueryTypes.SELECT});

One more thing : Use join instead of in

like image 43
Prayag C. Patel Avatar answered Nov 16 '22 23:11

Prayag C. Patel