From b2f7922f56c031747857a73c8794e2c1df6d8c4e Mon Sep 17 00:00:00 2001 From: Yash <84263676+yxshee@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:25:23 +0000 Subject: [PATCH] docs: fix query examples that throw or show the wrong SQL Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01NQDmA3mwGfL83pbg9WQ6ey --- docs/other-topics/scopes.md | 4 ++-- docs/querying/json.mdx | 12 ++++++------ docs/querying/operators.mdx | 2 +- docs/querying/raw-queries.mdx | 12 ++++++------ docs/querying/select-in-depth.md | 8 ++++---- docs/querying/sub-queries.md | 12 +++++++----- versioned_docs/version-6.x.x/other-topics/scopes.md | 4 ++-- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/docs/other-topics/scopes.md b/docs/other-topics/scopes.md index ec20af87f..5d828ac11 100644 --- a/docs/other-topics/scopes.md +++ b/docs/other-topics/scopes.md @@ -57,9 +57,9 @@ Project.init( }, }; }, - sequelize, - modelName: 'project', }, + sequelize, + modelName: 'project', }, ); ``` diff --git a/docs/querying/json.mdx b/docs/querying/json.mdx index 223acccb5..fb8e35c73 100644 --- a/docs/querying/json.mdx +++ b/docs/querying/json.mdx @@ -81,7 +81,7 @@ we use the `[]` syntax for array index access, and `.` for object property acces ```ts User.findAll({ where: { - 'gameData.passwords[0]': 0451, + 'gameData.passwords[0]': '0451', }, }); ``` @@ -94,10 +94,10 @@ produces -- when accessing array indexes in postgres, -- as Sequelize can sometimes use the "->" operator instead of "#>" for performance, -- which uses a different syntax for indexes & keys -"gameData"#>ARRAY['passwords','0'] = '0451'; +"gameData"#>ARRAY['passwords','0'] = '"0451"'; -- mysql & friends -JSON_EXTRACT(`gameData`, '$.passwords[0]') = '0451'; +JSON_EXTRACT(`gameData`, '$.passwords[0]') = '"0451"'; ``` ## Comparing JSON values @@ -228,10 +228,10 @@ produces ```sql -- postgres -"jsonAttribute"->'age' = 18 AND "jsonAttribute"->0 = '"1"'; +"jsonAttribute"->'age' = '18' AND "jsonAttribute"->0 = '"1"'; -- mysql & friends -JSON_EXTRACT(`jsonAttribute`, '$.age') = 18 AND JSON_EXTRACT(`jsonAttribute`, '$[0]') = '"1"'; +JSON_EXTRACT(`jsonAttribute`, '$.age') = '18' AND JSON_EXTRACT(`jsonAttribute`, '$[0]') = '"1"'; ``` ## Path Segment Escaping @@ -491,7 +491,7 @@ User.findAll({ SELECT * FROM "Users" WHERE "jsonAttribute"->'name' IS NULL OR "jsonAttribute"->'name' = 'null'; -- mysql & friends -SELECT * FROM "Users" WHERE JSON_EXTRACT(`jsonAttribute`, '$.name') IS NULL OR JSON_EXTRACT(`jsonAttribute`, '$.name') = '"null"'; +SELECT * FROM "Users" WHERE JSON_EXTRACT(`jsonAttribute`, '$.name') IS NULL OR JSON_EXTRACT(`jsonAttribute`, '$.name') = 'null'; ``` #### When to compare against either JSON `null` or SQL `NULL` diff --git a/docs/querying/operators.mdx b/docs/querying/operators.mdx index 09ac21925..8b83992c4 100644 --- a/docs/querying/operators.mdx +++ b/docs/querying/operators.mdx @@ -434,7 +434,7 @@ Post.findAll({ ``` ```sql -SELECT * FROM "posts" WHERE "title" LIKE 'Inflation is above 10\\%' ESCAPE '\\'; +SELECT * FROM "posts" WHERE "title" LIKE 'Inflation is above 10\%' ESCAPE '\'; ``` ### Regexp Operator diff --git a/docs/querying/raw-queries.mdx b/docs/querying/raw-queries.mdx index f766dcbb9..fb9d90a13 100644 --- a/docs/querying/raw-queries.mdx +++ b/docs/querying/raw-queries.mdx @@ -276,7 +276,7 @@ SELECT * FROM "public"."users" Using a Model class as an identifier will automatically use the table name of the Model: ```js -import { User, sql } from '@sequelize/core'; +import { Model, sql } from '@sequelize/core'; class User extends Model {} @@ -355,7 +355,7 @@ The separator can also be any SQL fragment: ```ts const values = ['active', 'pending']; -await sequelize.query(sql`SELECT * FROM projects WHERE status IN ${sql.join(values, sql`, `)}`); +await sequelize.query(sql`SELECT * FROM projects WHERE status IN (${sql.join(values, sql`, `)})`); ``` ```sql @@ -530,7 +530,7 @@ In supported dialects, using `sql.uuidV4` and `sql.uuidV1` will generate the dia In unsupported dialects, using these functions, except as [the default value of an attribute](../models/data-types.mdx#built-in-default-values-for-uuid), will throw an error. ```ts -sequelize.query(sql`INSERT INTO users (id) VALUES (${sql.uuidV4()})`); +sequelize.query(sql`INSERT INTO users (id) VALUES (${sql.uuidV4})`); ``` ```sql @@ -547,10 +547,10 @@ These functions are supported by the following dialects: `sql.random` generates a random float between 0 and 1, using the dialect-specific function to do so. ```ts -await sequelize.query(sql`INSERT INTO lottery (ticket) VALUES (${sql.random()})`); +await sequelize.query(sql`INSERT INTO lottery (ticket) VALUES (${sql.random})`); ``` -The SQL generated by `sql.random()` varies by dialect: +The SQL generated by `sql.random` varies by dialect: @@ -570,7 +570,7 @@ If you want to use the native `RANDOM()` function in SQLite, you can use [`sql`] :::note About MSSQL -Using `sql.random()` in an `ORDER BY` clause is a common way to get random ordering, +Using `sql.random` in an `ORDER BY` clause is a common way to get random ordering, however in MSSQL, it is better to use `NEWID()` for this purpose, as `RAND()` is evaluated only once per query, and will not give you random ordering. ::: diff --git a/docs/querying/select-in-depth.md b/docs/querying/select-in-depth.md index f7ca6e6a3..6d641f9d8 100644 --- a/docs/querying/select-in-depth.md +++ b/docs/querying/select-in-depth.md @@ -586,7 +586,7 @@ User.findAll({ }); ``` -You can remove that subquery, and revert back to a plain JOIN, by using the `subquery: false` option: +You can remove that subquery, and revert back to a plain JOIN, by using the `subQuery: false` option: ```ts User.findAll({ @@ -596,7 +596,7 @@ User.findAll({ }, limit: 2, // highlight-next-line - subquery: false, + subQuery: false, }); ``` @@ -617,7 +617,7 @@ User.findAll({ where: { id: { [Op.in]: sql` - SELECT DISTINCT "projects"."authorId" WHERE "projects"."name" = 'Project 1' + (SELECT DISTINCT "projects"."authorId" FROM "projects" WHERE "projects"."name" = 'Project 1') `, }, }, @@ -649,7 +649,7 @@ const author = await Author.findOne({ include: ['books'], }); -console.log(JSON.stringify(author[0])); +console.log(JSON.stringify(author)); ``` Console output: diff --git a/docs/querying/sub-queries.md b/docs/querying/sub-queries.md index eb5d28b3a..b8ead61f0 100644 --- a/docs/querying/sub-queries.md +++ b/docs/querying/sub-queries.md @@ -21,6 +21,7 @@ import { DataTypes, InferCreationAttributes, InferAttributes, + NonAttribute, } from '@sequelize/core'; import { Attribute, @@ -28,7 +29,7 @@ import { PrimaryKey, NotNull, HasMany, -} from '@sequelize/decorators-legacy'; +} from '@sequelize/core/decorators-legacy'; import { SqliteDialect } from '@sequelize/sqlite3'; class Post extends Model, InferCreationAttributes> { @@ -70,6 +71,7 @@ class Reaction extends Model { const sequelize = new Sequelize({ dialect: SqliteDialect, storage: ':memory:', + pool: { max: 1, idle: Infinity, maxUses: Infinity }, models: [Post, Reaction], }); ``` @@ -84,11 +86,11 @@ import { sql, Op } from '@sequelize/core'; Post.findAll({ where: { id: { - [Op.in]: sql` + [Op.in]: sql`( SELECT DISTINCT "postId" FROM "reactions" AS "reaction" WHERE "reaction"."type" = 'Laugh' - `, + )`, }, }, }); @@ -102,11 +104,11 @@ import { sql, Op } from '@sequelize/core'; function postHasReactionOfType(type: ReactionType) { return { id: { - [Op.in]: sql` + [Op.in]: sql`( SELECT DISTINCT "postId" FROM "reactions" AS "reaction" WHERE "reaction"."type" = ${type} - `, + )`, }, }; } diff --git a/versioned_docs/version-6.x.x/other-topics/scopes.md b/versioned_docs/version-6.x.x/other-topics/scopes.md index 19b2b165f..78fe00c12 100644 --- a/versioned_docs/version-6.x.x/other-topics/scopes.md +++ b/versioned_docs/version-6.x.x/other-topics/scopes.md @@ -47,9 +47,9 @@ Project.init( }, }; }, - sequelize, - modelName: 'project', }, + sequelize, + modelName: 'project', }, ); ```