Sequelize 'where' condition starts with 'AND' instead of 'WHERE' - sql

This doesn't make sence. Sequelize creates a where condition which starts with an AND instead of a WHERE.
I'm trying to execute this query:
var query = {
limit: 10,
order: [
['id', 'DESC']
],
//attributes: ['id', 'name', 'supplier_id', 'created_at', 'url'],
include: [
{
required: false,
model: models.termTaxonomies,
include: [{
model: models.term
}, {
attributes: ['id'],
model: models.taxonomy
}],
where: ["termRelationships.product_id IS NULL"],
},
models.image,
models.supplier
],
};
Which creates below SQL after executing above query with Product.findAll(query).
SELECT "product".*
,"termTaxonomies"."id" AS "termTaxonomies.id"
,"termTaxonomies"."taxonomy_id" AS "termTaxonomies.taxonomy_id"
,"termTaxonomies"."term_id" AS "termTaxonomies.term_id"
,"termTaxonomies"."parentId" AS "termTaxonomies.parentId"
,"termTaxonomies"."hierarchyLevel" AS "termTaxonomies.hierarchyLevel"
,"termTaxonomies"."distance" AS "termTaxonomies.distance"
,"termTaxonomies.termRelationships"."product_id" AS "termTaxonomies.termRelationships.product_id"
,"termTaxonomies.termRelationships"."term_taxonomy_id" AS "termTaxonomies.termRelationships.term_taxonomy_id"
,"termTaxonomies.term"."id" AS "termTaxonomies.term.id"
,"termTaxonomies.term"."name" AS "termTaxonomies.term.name"
,"termTaxonomies.term"."plural" AS "termTaxonomies.term.plural"
,"termTaxonomies.term"."sin_article" AS "termTaxonomies.term.sin_article"
,"termTaxonomies.term"."plu_article" AS "termTaxonomies.term.plu_article"
,"termTaxonomies.taxonomy"."id" AS "termTaxonomies.taxonomy.id"
,"images"."id" AS "images.id"
,"images"."deal_id" AS "images.deal_id"
,"images"."image" AS "images.image"
,"supplier"."id" AS "supplier.id"
,"supplier"."name" AS "supplier.name"
,"supplier"."url" AS "supplier.url"
,"supplier"."logo" AS "supplier.logo"
,"supplier"."clicks" AS "supplier.clicks"
,"supplier"."order" AS "supplier.order"
FROM (
SELECT "product"."id"
,"product"."name"
,"product"."subtitle"
,"product"."url"
,"product"."prod_specs"
,"product"."prod_desc"
,"product"."supplier_id"
,"product"."created_at"
,"product"."updated_at"
,"product"."active"
FROM "products" AS "product"
ORDER BY "product"."id" DESC LIMIT 10
) AS "product"
LEFT JOIN (
"term_relationships" AS "termTaxonomies.termRelationships" LEFT JOIN "term_taxonomies" AS "termTaxonomies" ON "termTaxonomies"."id" = "termTaxonomies.termRelationships"."term_taxonomy_id"
) ON "product"."id" = "termTaxonomies.termRelationships"."product_id"
AND termRelationships.product_id IS NULL
LEFT JOIN "terms" AS "termTaxonomies.term" ON "termTaxonomies"."term_id" = "termTaxonomies.term"."id"
LEFT JOIN "taxonomies" AS "termTaxonomies.taxonomy" ON "termTaxonomies"."taxonomy_id" = "termTaxonomies.taxonomy"."id"
LEFT JOIN "images" AS "images" ON "product"."id" = "images"."deal_id"
LEFT JOIN "suppliers" AS "supplier" ON "product"."supplier_id" = "supplier"."id"
ORDER BY "product"."id" DESC;
Check 6th last row (AND termRelationships.product_id IS NULL).
The tables for this case:
I'm trying to get all Products with their suppliers and offers, which aren't categorized yet (so which are not inside termTaxonomies for now).
It's easy to do this with a sql query, but now we are using an ORM (Sequelize), we would love to just use it completely. Anyone who can help us out?
Guess it's a bit too much to post all of my models below so I'll try to keep is short:
associations product model:
product.hasMany(_models.offer, {
foreignKey: 'product_id'
});
product.belongsToMany(_models.termTaxonomies, {
through: _models.termRelationships,
foreignKey: 'product_id'
});
product.hasMany(_models.image, {
foreignKey: 'deal_id'
});
product.belongsTo(_models.supplier, {
foreignKey: 'supplier_id'
});
associations offer model:
offer.belongsTo(_models.product, {
foreignKey: 'product_id'
});
offer.hasMany(_models.sentDeals, {
foreignKey: 'offer_id'
});
offer.hasMany(_models.transaction, {
foreignKey: 'offer_id'
});
associations supplier model:
supplier.hasMany(_models.product, {
foreignKey: 'supplier_id'
});
supplier.hasMany(_models.scraperLog, {
foreignKey: 'scraper_id'
});
associations termTaxonomies model:
termTaxonomies.belongsToMany(_models.product, {
through: _models.termRelationships,
foreignKey: 'term_taxonomy_id',
});
termTaxonomies.belongsTo(_models.term, {
foreignKey: 'term_id',
});
termTaxonomies.belongsTo(_models.taxonomy, {
foreignKey: 'taxonomy_id',
});

The and is the continuation of the LEFT JOIN:-
LEFT JOIN (
"term_relationships" AS "termTaxonomies.termRelationships" LEFT JOIN "term_taxonomies" AS "termTaxonomies" ON "termTaxonomies"."id" = "termTaxonomies.termRelationships"."term_taxonomy_id"
) ON "product"."id" = "termTaxonomies.termRelationships"."product_id"
AND termRelationships.product_id IS NULL

Related

TypeORM The results of the conditional query were not match

I set the where condition in the find statement, but the results of the conditional query where not match.
The find query:
async getRegister() {
const result = await this.registerModel.findOne({
where: {
session: 1,
user: 2,
},
select: ['id', 'created_time'],
});
return result;
}
The register entity:
#Entity('register')
export class Register {
#PrimaryGeneratedColumn()
id: number;
#Column({
type: 'text',
nullable: true,
unique: true,
})
no: string;
……
#ManyToOne(type => User, user => user.registers)
user: User;
#OneToOne(type => Session)
#JoinColumn()
session: Session;
}
The formatted SQL statement:
query: SELECT DISTINCT "distinctAlias"."Register_id" AS "ids_Register_id" FROM (SELECT "Register"."id" AS "Register_id", "Register"."created_time" AS "Register_created_time" FROM "register" "Register" INNER JOIN "session" "Register__Register_session" ON "Register__Register_session"."id"="Register"."sessionId" AND ("Register__Register_session"."deleted_time" IS NULL) INNER JOIN "user" "Register__Register_user" ON "Register__Register_user"."id"="Register"."userId" AND ("Register__Register_user"."deleted_time" IS NULL) WHERE "Register"."deleted_time" IS NULL) "distinctAlias" ORDER BY "Register_id" ASC LIMIT 1
query: SELECT "Register"."id" AS "Register_id", "Register"."created_time" AS "Register_created_time" FROM "register" "Register" INNER JOIN "session" "Register__Register_session" ON "Register__Register_session"."id"="Register"."sessionId" AND ("Register__Register_session"."deleted_time" IS NULL) INNER JOIN "user" "Register__Register_user" ON "Register__Register_user"."id"="Register"."userId" AND ("Register__Register_user"."deleted_time" IS NULL) WHERE ( "Register"."deleted_time" IS NULL ) AND ( "Register"."id" IN (7) )
I could not find the parameter set
where: {
session: 1,
user: 2,
},
When I use sql, I get the right results.
await this.registerModel.query(
`select id, created_time from register where userId = ${userId} and sessionId = ${sessionId}`
);
I have fixed this issuse.
First modified the registered entity
#Entity('register')
export class Register {
#PrimaryGeneratedColumn()
id: number;
#Column({
type: 'text',
nullable: true,
unique: true,
comment: '准考证号',
})
no: string;
……
#ManyToOne(type => User, user => user.id)
user: User;
#ManyToOne(type => Session, session => session.id)
session: Session;
}
Last modified the find query:
const result = await this.registerModel.findOne({
where: {
user: {
id: userId,
},
session: {
id: sessionId,
},
},
select: ['id', 'created_time'],
});
return result;

SQL query to PostgeSQL tables [duplicate]

This question already has answers here:
PostgreSQL "Column does not exist" but it actually does
(6 answers)
sql statement error: "column .. does not exist"
(1 answer)
Postgresql Column Not Found, But Shows in Describe
(1 answer)
Closed 2 years ago.
I have a problem.
SELECT *
FROM posts CROSS JOIN public."postReactions" AS reactions
WHERE posts.userId = '423abb9e-a00d-4045-9e88-4a85897f67e4'
But the response from DB is like 'ERROR: column posts.userid doesn't exist.
LINE 3: WHERE posts.userId = '423abb9e-a00d-4045-9e88-4a85897f67e4'.
Result of CROSS JOIN below:
Posts table
PostReactions table
Sequelize models below:
export default models => {
const {
User,
Post,
PostReaction,
PostNegativeReaction,
Comment,
Image
} = models;
User.hasMany(Post);
User.hasMany(PostReaction);
Post.belongsTo(User);
Post.hasMany(PostReaction);
PostReaction.belongsTo(Post);
PostReaction.belongsTo(User);
};
This is my associations:
export default {
up: (queryInterface, Sequelize) => queryInterface.sequelize
.transaction(transaction => Promise.all([
queryInterface.addColumn('posts', 'userId', {
type: Sequelize.UUID,
references: {
model: 'users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
}, { transaction }),
queryInterface.addColumn('postReactions', 'userId', {
type: Sequelize.UUID,
references: {
model: 'users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
}, { transaction }),
queryInterface.addColumn('postReactions', 'postId', {
type: Sequelize.UUID,
references: {
model: 'posts',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL'
}, { transaction }),
])),
down: queryInterface => queryInterface.sequelize
.transaction(transaction => Promise.all([
queryInterface.removeColumn('posts', 'userId', { transaction }),
queryInterface.removeColumn('postReactions', 'userId', { transaction }),
queryInterface.removeColumn('postReactions', 'postId', { transaction })
]))
};
MERN stack.
Main problem: how to do a request to db that will receive the response with POSTS body (only liked by the current user).
This would be possible if the table were created using escaped column names . . . "postId" rather than postId.
I suspect that is the problem. The best solution is to recreate the table and not escape any identifiers, ever. Alternatively, you can use the escaped column name.

How do I write this Raw Postgres query in sequelize

I Have this postgres RAW query, I wanted to write it in Sequelize. How do I do this since I have less idea about Writing queries having JOINS in Sequelize. I have made models and Associations.
These are models and associations.
TestParticipant.hasMany(ParticipantHistory, {
sourceKey: "id",
foreignKey: "participantId",
as: "paticipantStatuses"
})
ParticipantHistory.belongsTo(TestParticipant, {
foreignKey: "participantId",
as: "paticipantStatuses"
})
This is the raw Query I wanna transform into Sequelize query
SELECT participant_histories.participant_id,
participant_histories.created_at,participant_histories.previous_status,
participant_histories.status,test_participants.test_type_id,test_participants.id,
test_participants.email,test_participants.scheduled_at,test_participants.valid_till,
test_participants.is_proctored
FROM test_participants
INNER JOIN participant_histories ON test_participants.id=participant_histories.participant_id
WHERE user_id='${userId}'
AND participant_histories.status='${activity}'
AND participant_histories.created_at>='${isoDate}'
Because I don't see model definitions in the post I suggest only something like this:
// First of all you should correct an alias for TestParticipant like this
ParticipantHistory.belongsTo(TestParticipant, {
foreignKey: "participantId",
as: "paticipant"
})
const rows = await ParticipantHistory.findAll({
raw: true,
attributes: ['participant_id', 'created_at', 'previous_status', 'status'],
where: {
status: activity,
created_at: {
[Op.gte]: isoDate
}
},
include: [{
required: true // this turns into INNER JOIN
model: TestParticipant,
attributes: ['test_type_id', 'id', 'email', 'scheduled_at', 'valid_till', 'is_proctored'],
as: 'participant',
where: {
user_id: userId
}
}]
})

select next and previous records in Sequelize based on createdAt

What I want: fetch one record from the database based on its ID (UUID) and retrieve next and previous records too
Associations: posts N:M categories, posts 1:N comments, posts N:1 users, posts N:M tags, posts 1:N post_views
Current solution:
const post = await PostModel.findByPk(id, {
include: [
{ model: CategoryModel, attributes: ['title'] },
{ model: TagModel },
{ model: CommentModel, include: { model: UserModel } },
{ model: PostViewModel },
{ model: UserModel, attributes: ['fullname', 'id', 'avatar'] }
]
});
const nextPost = await PostModel.findOne({
where: { createdAt: {
[Op.gt]: post.createdAt
}}
const prevPost = await PostModel.findOne({
where: { createdAt: {
[Op.lt]: post.createdAt
}}
});
but I think this is not efficient and good.

Join 3 Tables with Sequelize

I'm trying to convert below raw query to ORM but couldn't achieve anything.
raw query
select * from "Invoices" I
LEFT JOIN "Requests" R ON R."id" = I."requestId"
LEFT JOIN "Supps" S ON S."requestId" = R."id"
where S."confirm" = 'OK'
What I've tried:
Requests.belongsTo(Invoices, { foreignKey: "id" });
Supps.belongsTo(Requests, { foreignKey: "requestId" });
Supps.hasMany(Requests, { foreignKey: "requestId" });
Invoices.hasMany(Requests, { foreignKey: "requestId" });
Invoices.findOne({
include: [{ model: Requests, required: false }, { model: Supps, required: false }],
where: { Sequelize.col("Supps.confirm"): { "OK" } }
}).then(data => {
console.log(data);
})
But this generates a very very long query with multiple sub queries and wrong data
For sure you will need to add also the sourceKey in th associations. (doc)
Supps.hasMany(Requests, { foreignKey: "requestId" ,sourceKey: '{ID}' });
Invoices.hasMany(Requests, { foreignKey: "requestId",sourceKey: '{ID}' });
Also you can add {raw:true} to get a one level json response .
Invoices.findOne({
include: [{ model: Requests, required: false }, { model: Supps, required: false }],
where: { Sequelize.col("Supps.confirm"): { "OK" } },
raw:true
}).then(data => {
console.log(data);
});