I am a new one working in Sequelize. I do not know how to change the query to sequelize
SELECT a.* FROM employees a,emp_dept_details b where b.Dept_Id=2 AND a.Emp_No = b.Emp_Id
Emp_No is Primary Key in Employee table
EmpDeptDetails.hasMany(Emp, {foreignKey: 'Emp_No'})
Emp.belongsTo(EmpDeptDetails, {foreignKey: 'Emp_No'})
I am not sure but try out the below query
Assuming
You have the relationships between the 2 tables defined in the models class.
You have Model Employee for employees table and EmployeeDeptDetails for emp_dept_details table. Change them to your respective names before trying.
Employee.findAll({
include: [{
model: EmployeeDeptDetails,
where: { Emp_Id: Sequelize.col('employees.Emp_No'), Dept_Id: 2 }
}]
});
Related
I have (almost) identical multiple rows in a table in which createdAt is different.
I am using sequelize typescript as ORM for mysql data.
Is there a way how I can use group by on 2 columns of the table lets say colA, colB but get the latest entry in that group?
an equivalent MySQL query for this would be :
SELECT * FROM tableName
WHERE id IN (
SELECT MAX(id)
FROM tableName
group by tableName.colA,
tableName.colB
);
But I don't want to use id as max, and how can I get it in sequelize typescript?
With the normal group by col such as:
MyModel.findAll({
where: {
createdAt: { [Op.lt]: today }
},
group: ['colA','colB']
})
I was able to retrieve the grouped columns' first createdAt entry whereas I want the last entry.
Any help would be appreciated.
Plesae try with order by
MyModel.findAll({
where: {
createdAt: { [Op.lt]: today }
},
order : [['createdAt','DESC']],
group: ['colA','colB']
})
I want to associate two tables say tableA(user) tableB(tasks). I have created the foreign key (say userId) in tableB of datatype array Integer. Now I want to associate A and B table but is showing
SequelizeDatabaseError: operator does not exist: integer[] = integer
I tried this :
db.tableB.belongsTo(db.tableA, { foreignKey: 'userId'});
db.tableB.findAll({
where:{}.
include : [{
model: db.tableA
}]
})
Is it possible to make an association in sequelize from a model with a column which is array of json
timeLine:[
{
userId:2,
status:Started
},
{
userId:3,
status:Ended
}
]
timeLine is a column in case table.
I want a cases which was started by given user.
On a high level you need to create two models.
User
Case
User would have a foreign key id to the Case table.
Next in the Case model, establish a one-to-many relationship so that you can do a Case.timeline to pull up the array of users.
// example of how to establish the one-to-many relationship
Case.hasMany(models.user, {
as: 'timeline',
foreignKey: {
name: 'caseId',
allowNull: false
}
});
I have defined this models with Sequelize.js v3:
Model1: cities
Model2: districts
this.belongsTo(models.cities, {as: "city", foreignKey: 'city_id'});
Model3: streets
this.belongsTo(models.districts, {as: "district", foreignKey: 'district_id'});
Model4: houses
this.belongsTo(models.streets, {as: "street", foreignKey: 'street_id'});
Is there a way to set a relation between the models "cities" and "houses", so that chosen a city only the houses of that city are selected?
Or do I have to add the "city_id" column in the tables "districts" and "streets"?
I ask this because in the app I have to implement a page with only two comboboxes: city and houses.
You'll need to go all the way dowm from cities to houses. And for that you actually need to make the association from top to bottom, right now you have it the other way arround. Let me explain.
With your actual associations your models calls each other on this way:
houses -> streets -> districts -> cities
Because houses depends from streets and so on. What we need is all to go to the other.
cities -> districts -> streets -> houses
Model1: cities
this.hasMany(models.districts, {as: "districts", foreignKey: 'city_id'});
And do the same for districts and streets.
Now for what you want we can do the following query.
cities.findAll({
include: [{
model: model.districts,
as: 'districts',
include: [{
model: model.streets,
as: 'streets',
include: [{
model: model.houses,
as: 'houses',
where: {} //your condition here
}]
}]
}]
});
Now, the other way could be as you mention, that put an association directly from cities to houses, but that really depends on your schema and business model.
I am using Sequelize for PostgresSQL. For example: I've got two tables Friendship and User. To make friendship possible for every user, I've got two relations n:n.
Associations in Sequelize
User.hasMany(Friendship,
{
as: "friendshipsAsked",
foreignKey: "asked_user_id",
joinTableName: "friendship"
});
User.hasMany(Friendship,
{
as: "friendshipsAsking",
foreignKey: "asking_user_id",
joinTableName: "friendship"
});
Friendship.belongsTo(User,
{
...
});
Friendship.belongsTo(User,
{
...
});
My biggest problem is querying friendships for user. When I've got user model, I have to get user.getFriendshipsAsking() and user.getFriendshipsAsked() and then merge both.
Is there any possibility to change relations so I can have user.getFriendships() in SQL such that it will perform like SELECT * FROM frienships WHERE requesting_user_id = 'user.id' OR requested_user_id = 'user.id'?
I would like to have a better solution to model friendship between users in the database?