Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include Sequelize.col s.replace is not a function #12361

Open
2 of 7 tasks
habitshine opened this issue Jun 10, 2020 · 11 comments
Open
2 of 7 tasks

include Sequelize.col s.replace is not a function #12361

habitshine opened this issue Jun 10, 2020 · 11 comments

Comments

@habitshine
Copy link

Issue Description

What are you doing?

I get an exception when querying data through the association table and combining fields.
For example, collections is the main table, collection_products is the associated table, and products is the associated word table.
The query statement is as follows:

await model.collections.findOne({
            include: [
                'images',
                {
                    model: model.collection_products,
                    as: 'products',
                    attributes: ['id', Sequelize.sequelize.col('product.title')],
                    include: {
                        model: model.products,
                        as: 'product'
                    }
                }
            ],
            where: {
                handle: this.ctx.params.handle
            }
        })

What do you expect to happen?

输出结果

{
    "id": "9259e355-b30e-47ab-9da6-6289c65b98b0",
    "title": "Photo Engraved Necklaces",
    "images": [
        {
            "id": "e4b4ebda-726c-42dc-868d-fc2984f8b4bc",
        }
    ],
    "products": [
        {
            "id": "4b750c88-ddaa-4605-9f5c-318011651d25",
			"title": "Engraved Double Heart Birthstone Promise Ring Silver",
			"handle": "engraved-double-heart-birthstone-promise-ring-silver",
        },
        {
            "id": "adb04f9e-d35a-49d1-a6b3-ef879a62663d",
			"title": "I Love You Bracelet In 100 Languages Projection Engraved Bracelet Four",
			"handle": "i-love-you-bracelet-in-100-languages-projection-engraved-bracelet-four",
        }
    ]
}

What is actually happening?

An error message was output

s.replace is not a function

Additional context

image

Environment

  • Sequelize version: 5.21.12
  • Node.js version: v13.12.0
  • Operating System: windows 10

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@sushantdhiman
Copy link
Contributor

Can you include the stack trace? Also what is the value of this.ctx.params.handle?

@habitshine
Copy link
Author

您可以包括堆栈跟踪吗?还有什么价值this.ctx.params.handle

this.ctx.params.handle is a filtered index that does not affect the query

node_modules\_sequelize@5.21.12@sequelize\lib\utils.js:354:12
Object.removeTicks
node_modules\_sequelize@5.21.12@sequelize\lib\dialects\abstract\query-generator\helpers\quote.js:50:35
Object.quoteIdentifier
node_modules\_sequelize@5.21.12@sequelize\lib\dialects\abstract\query-generator.js:891:24
PostgresQueryGenerator.quoteIdentifier
node_modules\_sequelize@5.21.12@sequelize\lib\dialects\abstract\query-generator.js:1517:74
node_modules\_sequelize@5.21.12@sequelize\lib\dialects\abstract\query-generator.js:1481:52
PostgresQueryGenerator.generateInclude
node_modules\_sequelize@5.21.12@sequelize\lib\dialects\abstract\query-generator.js:1183:34
PostgresQueryGenerator.selectQuery
node_modules\_sequelize@5.21.12@sequelize\lib\query-interface.js:1122:27
QueryInterface.select
node_modules\_sequelize@5.21.12@sequelize\lib\model.js:1759:34
node_modules\_bluebird@3.7.2@bluebird\js\release\util.js:16:23
tryCatcher
node_modules\_bluebird@3.7.2@bluebird\js\release\promise.js:547:31
Promise._settlePromiseFromHandler
node_modules\_bluebird@3.7.2@bluebird\js\release\promise.js:604:18
Promise._settlePromise
node_modules\_bluebird@3.7.2@bluebird\js\release\promise.js:649:10
Promise._settlePromise0
node_modules\_bluebird@3.7.2@bluebird\js\release\promise.js:729:18
Promise._settlePromises
node_modules\_bluebird@3.7.2@bluebird\js\release\async.js:93:12
_drainQueueStep
node_modules\_bluebird@3.7.2@bluebird\js\release\async.js:86:9
_drainQueue
node_modules\_bluebird@3.7.2@bluebird\js\release\async.js:102:5
Async._drainQueues
node_modules\_bluebird@3.7.2@bluebird\js\release\async.js:15:14
Immediate.Async.drainQueues [as _onImmediate]
internal/timers.js:456:21
processImmediate
node_modules\_bluebird@3.7.2@bluebird\js\release\debuggability.js:482:19
Promise.longStackTracesCaptureStackTrace [as _captureStackTrace]
node_modules\_bluebird@3.7.2@bluebird\js\release\promise.js:261:17
Promise._then
node_modules\_bluebird@3.7.2@bluebird\js\release\promise.js:154:17
Promise.then
node_modules\_sequelize@5.21.12@sequelize\lib\model.js:1757:8
Function.findAll
node_modules\_sequelize@5.21.12@sequelize\lib\model.js:1926:17
Function.findOne
app\controller\collection.js:8:52
CollectionsController.index
node_modules\_egg-core@4.17.4@egg-core\lib\utils\index.js:44:21
Object.callFn
node_modules\_egg-core@4.17.4@egg-core\lib\loader\mixin\controller.js:87:20
Object.classControllerMiddleware
node_modules\_@eggjs_router@2.0.0@@eggjs\router\lib\utils.js:12:21
Object.callFn
node_modules\_@eggjs_router@2.0.0@@eggjs\router\lib\egg_router.js:322:18
wrappedController
node_modules\_koa-compose@3.2.1@koa-compose\index.js:44:32
dispatch
node_modules\_koa-compose@3.2.1@koa-compose\index.js:45:18
next
node_modules\_@eggjs_router@2.0.0@@eggjs\router\lib\router.js:190:18
node_modules\_koa-compose@3.2.1@koa-compose\index.js:44:32
dispatch
node_modules\_koa-compose@3.2.1@koa-compose\index.js:36:12
node_modules\_@eggjs_router@2.0.0@@eggjs\router\lib\router.js:195:33
dispatch
node_modules\_koa-compose@4.1.0@koa-compose\index.js:42:32
dispatch
node_modules\_postgraphile@4.7.0@postgraphile\build\postgraphile\http\createPostGraphileHttpRequestHandler.js:432:20
requestHandler
node_modules\_postgraphile@4.7.0@postgraphile\build\postgraphile\http\koaMiddleware.js:26:24
Object.exports.middleware
node_modules\_postgraphile@4.7.0@postgraphile\build\postgraphile\http\createPostGraphileHttpRequestHandler.js:716:36
middleware
node_modules\_koa-compose@4.1.0@koa-compose\index.js:42:32
dispatch
node_modules\_egg-postgraphile@1.0.0@egg-postgraphile\app.js:119:22
node_modules\_koa-compose@4.1.0@koa-compose\index.js:42:32
dispatch
app\middleware\request_verify.js:8:22
node_modules\_koa-compose@4.1.0@koa-compose\index.js:42:32
dispatch
node_modules\_egg-development@2.5.0@egg-development\app\middleware\egg_loader_trace.js:9:56
node_modules\_koa-compose@4.1.0@koa-compose\index.js:42:32
dispatch
node_modules\_egg-i18n@2.1.1@egg-i18n\app\middleware\i18n.js:13:12
i18n

@sushantdhiman
Copy link
Contributor

col should be called with column name title, not product.title. Try using literal instead of col. Although I note that this is a bug, it should throw some meaningful error

@habitshine
Copy link
Author

col should be called with column name title, not product.title. Try using literal instead of col. Although I note that this is a bug, it should throw some meaningful error

But I searched the information in the community. Someone wrote this in the v4 version, and it worked. The difference is that his database is mysql, mine is postgresql.

@sushantdhiman
Copy link
Contributor

I tried this on both mysql and postgres, it fails with same error. You have to try literal.

@sushantdhiman
Copy link
Contributor

sushantdhiman commented Jun 12, 2020

SSCCE

Only fails when used in include

(async () => {                                               
  const User = sequelize.define("user", {  
    name: DataTypes.STRING,       
  });                                                        
                                       
  const Project = sequelize.define("project", {                                  
    name: DataTypes.STRING,                                  
  });                                  
                                  
  User.hasMany(Project);  
  Project.belongsTo(User);  
  
  await sequelize.sync({ force: true });  
  
  const result = await User.findOne({  
    include: [  
      {  
        model: Project,  
        attributes: ["id", Sequelize.col("project.name")],  
      },  
    ],  
    where: {  
      name: "abc",  
    },  
  });  
  
  console.log(result);  
})().catch((e) => console.error(e));  
Executing (default): DROP TABLE IF EXISTS `projects`;
Executing (default): DROP TABLE IF EXISTS `users`;
Executing (default): DROP TABLE IF EXISTS `users`;
Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `users`
Executing (default): DROP TABLE IF EXISTS `projects`;
Executing (default): CREATE TABLE IF NOT EXISTS `projects` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`userId`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `projects`
TypeError: s.replace is not a function
    at Object.removeTicks (/var/projects/oss/sequelize/lib/utils.js:347:12)
    at Object.quoteIdentifier (/var/projects/oss/sequelize/lib/dialects/abstract/query-generator/helpers/quote.js:47:35)
    at MySQLQueryGenerator.quoteIdentifier (/var/projects/oss/sequelize/lib/dialects/abstract/query-generator.js:891:24)
    at /var/projects/oss/sequelize/lib/dialects/abstract/query-generator.js:1519:74
    at Array.map (<anonymous>)
    at MySQLQueryGenerator.generateInclude (/var/projects/oss/sequelize/lib/dialects/abstract/query-generator.js:1483:52)
    at MySQLQueryGenerator.selectQuery (/var/projects/oss/sequelize/lib/dialects/abstract/query-generator.js:1183:34)
    at MySQLQueryInterface.select (/var/projects/oss/sequelize/lib/dialects/abstract/query-interface.js:939:27)
    at Function.findAll (/var/projects/oss/sequelize/lib/model.js:1741:47)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

@github-actions
Copy link
Contributor

github-actions bot commented Nov 2, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 2, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@satishakumar-simpplr
Copy link

Getting this error while doing bulk insert

Model.bulkCreate(listRecordToInsert, {updateOnDuplicate: ['id']}).catch((error) => {
                if(error){
                    console.log(error);
                }
            });
TypeError: s.replace is not a function
    at Object.removeTicks (service\node_modules\sequelize\lib\utils.js:281:12)
    at PostgresQueryGenerator.quoteIdentifier [as _quoteIdentifier] (service\node_modules\sequelize\lib\dialects\postgres\query-generator.js:656:33)
    at PostgresQueryGenerator.quoteIdentifier (service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:645:19)
    at service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:239:68
    at Array.map (<anonymous>)
    at PostgresQueryGenerator.bulkInsertQuery (service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:239:49)
    at PostgresQueryInterface.bulkInsert (service\node_modules\sequelize\lib\dialects\abstract\query-interface.js:335:68)
    at recursiveBulkCreate (service\node_modules\sequelize\lib\model.js:1657:52)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Function.bulkCreate (service\node_modules\sequelize\lib\model.js:1746:12)

@maramizo
Copy link

Getting this error while doing bulk insert

Model.bulkCreate(listRecordToInsert, {updateOnDuplicate: ['id']}).catch((error) => {
                if(error){
                    console.log(error);
                }
            });
TypeError: s.replace is not a function
    at Object.removeTicks (service\node_modules\sequelize\lib\utils.js:281:12)
    at PostgresQueryGenerator.quoteIdentifier [as _quoteIdentifier] (service\node_modules\sequelize\lib\dialects\postgres\query-generator.js:656:33)
    at PostgresQueryGenerator.quoteIdentifier (service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:645:19)
    at service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:239:68
    at Array.map (<anonymous>)
    at PostgresQueryGenerator.bulkInsertQuery (service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:239:49)
    at PostgresQueryInterface.bulkInsert (service\node_modules\sequelize\lib\dialects\abstract\query-interface.js:335:68)
    at recursiveBulkCreate (service\node_modules\sequelize\lib\model.js:1657:52)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Function.bulkCreate (service\node_modules\sequelize\lib\model.js:1746:12)

Likewise.

@shreyansh-zazz
Copy link

Getting this error while doing bulk insert

Model.bulkCreate(listRecordToInsert, {updateOnDuplicate: ['id']}).catch((error) => {
                if(error){
                    console.log(error);
                }
            });
TypeError: s.replace is not a function
    at Object.removeTicks (service\node_modules\sequelize\lib\utils.js:281:12)
    at PostgresQueryGenerator.quoteIdentifier [as _quoteIdentifier] (service\node_modules\sequelize\lib\dialects\postgres\query-generator.js:656:33)
    at PostgresQueryGenerator.quoteIdentifier (service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:645:19)
    at service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:239:68
    at Array.map (<anonymous>)
    at PostgresQueryGenerator.bulkInsertQuery (service\node_modules\sequelize\lib\dialects\abstract\query-generator.js:239:49)
    at PostgresQueryInterface.bulkInsert (service\node_modules\sequelize\lib\dialects\abstract\query-interface.js:335:68)
    at recursiveBulkCreate (service\node_modules\sequelize\lib\model.js:1657:52)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Function.bulkCreate (service\node_modules\sequelize\lib\model.js:1746:12)

Getting same issue, any update on this thread?

@mhuggins
Copy link

mhuggins commented Jun 11, 2022

I'm running into this error when I use a calculated index field in conjunction with upsert on a Postgres database.

Report.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
    userId: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    createdAt: DataTypes.DATE,
    updatedAt: DataTypes.DATE,
  },
  {
    sequelize,
    tableName: 'reports',
    indexes: [
      {
        name: 'idx_managed_report',
        fields: [sequelize.literal('("userId" is null)'), 'name'], // this line here
        unique: true,
        where: { userId: null },
      },
    },
  },
);

await Report.upsert({ name: 'All Data' });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants