Tag: sequelize.js

Nodjs sequalize如何获得最后插入ID

我正在使用Sequalize与nodejs在表中插入一行,现在我想要最后插入的ID,我想要一些像“mysql_insert_id()”,这里是我的代码: function registerAgent(agent_data,request_key,res,next) { models.agent.create({creator_id: agent_data.userid }, { fields: [ 'creator_id'] }).then(function(user) { console.log(user); }); };

如何注入与Server.inject模拟testinghapi

我想用lab来testinghapi路由,我正在使用mysql数据库。 使用Server.inject来testing路由的问题是,我不能模拟数据库,因为我没有调用包含处理函数的文件,所以如何注入处理程序中的模拟数据库?

在Node.js中处理回滚的MySQL事务

我正在处理一个两三天的问题,我真的希望你能帮助我。 这是一个基于node.js的API,使用MySQL sequelize 。 在某些API调用中,代码会启动locking某些表的SQL事务,如果我同时向API发送多个请求, LOCK_WAIT_TIMEOUT得到LOCK_WAIT_TIMEOUT错误。 var SQLProcess = function () { var self = this; var _arguments = arguments; return sequelize.transaction(function (transaction) { return doSomething({transaction: transactioin}); }) .catch(function (error) { if (error && error.original && error.original.code === 'ER_LOCK_WAIT_TIMEOUT') { return Promise.delay(Math.random() * 1000) .then(function () { return SQLProcess.apply(self, _arguments); }); } else { throw […]

使用关联来升级更新

在sequelize中可以创build一个行,所有它的关联一次去这样的: return Product.create({ title: 'Chair', User: { first_name: 'Mick', last_name: 'Broadstone' } }, { include: [ User ] }); 有更新的等价物吗? 我试过了 model.user.update(req.body.user, {where: {id: req.user.user_id}, include: [model.profile]}) 但它只是更新用户 这样做是为了创造作品 model.user.create(user, {transaction: t, include: [model.profile]})

合并重新出现的错误中的堆栈跟踪

我在这里重新抛出Sequelize承诺(蓝鸟)的错误。 首先,这是为了改变错误信息,但是当它出现时,这也产生了更多信息的堆栈跟踪。 这是类似的 sequelize.sync().catch(originalError => { const rethrownError = new Error(originalError.msg + ': ' + originalError.sql); throw rethrownError; }); 其中originalError.stack不包含导致错误的行,但是它包含重要的信息,它来源于Sequelize和MySQL驱动程序: SequelizeDatabaseError: ER_KEY_COLUMN_DOES_NOT_EXITS: Key column 'NonExisting' doesn't exist in table at Query.formatError (…\node_modules\sequelize\lib\dialects\mysql\query.js:175:14) at Query._callback (…\node_modules\sequelize\lib\dialects\mysql\query.js:49:21) at Query.Sequence.end (…\node_modules\mysql\lib\protocol\sequences\Sequence.js:85:24) at Query.ErrorPacket (…\node_modules\mysql\lib\protocol\sequences\Query.js:94:8) at Protocol._parsePacket (…\node_modules\mysql\lib\protocol\Protocol.js:280:23) at Parser.write (…\node_modules\mysql\lib\protocol\Parser.js:74:12) at Protocol.write (…\node_modules\mysql\lib\protocol\Protocol.js:39:16) at Socket.<anonymous> (…\node_modules\mysql\lib\Connection.js:109:28) at emitOne […]

在Sequelize.js中调用存储过程

我search了文档,并试图谷歌,但我没有find一个直接的答案的问题: 如何在Sequelize中调用存储过程? 我已经search了Sequelize的文档,但我甚至没有find“程序”一词的踪迹。 我得到的最接近的是这个bug-report-turned-feature-request: https : //github.com/sequelize/sequelize/issues/959 从链接引用: 我想象的会很棒: sequelize.query('CALL calculateFees();').success( function (settingName1, settingName2, settingName3, users) { }); 他们提到可以调用存储过程,但是没有提供语法。 任何人都可以给我一个正确的语法的例子吗? 谢谢。

使用NodeJS进行续集不能连接有限制的表

我试图实现一个简单的查询,应该看起来像这样: select * from property join entity_area on property.id=entity_area.entity_id and entity_area.area_id=1 where property.price>300000 limit 12 非常简单:我想获得join的结果,然后限制到12。 在Sequelize中,我使用以下函数: return models.property.findAll( { where: ["price>=?", 300000], include: [ { model:models.entity_area, where: { area_id:1 } } ], limit:12 }) 但是这个代码生成以下sql: select property.*, entity_area.* from (select * from property where property.price>300000 limit 12) join entity_area on property.id=entity_area.entity_id and entity_area.area_id=1 这与我想要做的完全不同的逻辑,因为在生成的SQL它首先获得任何12个结果,然后尝试joinentity_area,当然随机12结果不一定匹配entity_area,所以我'没有结果回来。 […]

Sequelize – findOne()。success()是未定义的

我正在关注passport.js文档以创build一个使用Sequelize作为ORM的LocalStrategy作为我的postgres数据库。 然而,在authentication过程中,做User.findOne(…).success(function(user){…]}); 在我的login.js模块返回undefined 。 我究竟做错了什么? user.js的: var pg = require('pg'); var Sequelize = require('sequelize'); var sequelize = new Sequelize('database', 'username', 'password', { host: 'localhost', port: 5432, dialect: 'postgres' }); var User = sequelize.define('users', { username: Sequelize.STRING, password: Sequelize.STRING }); User.sync(); module.exports = User; 我的login.js(路由器) var express = require('express'); var router = express.Router(); var passport = […]

Postgres方言不工作在续集-m

运行Sequelize -m 在我的config.json "development": { "username": "root", "password": null, "database": "**********", "dialect": "postgres", "protocol": "postgres", "port": 5432, "host": "127.0.0.1" }, 出错: sequelize -m Loaded configuration file "config/config.json". Using environment "development". /usr/local/lib/node_modules/sequelize/lib/transaction-manager.js:10 throw new Error("The dialect " + sequelize.getDialect() + " is not support ^ Error: The dialect postgres is not supported. at new module.exports (/usr/local/lib/node_modules/sequelize/lib/transaction-manager.js:10:11) […]

以错误的顺序重新排列表格

我在我的nodejs应用程序中使用sequelize ORM,它似乎是在我sequelize.sync({force: true})以错误的顺序丢弃表 例如,用: var StationEntity = sequelize.define('Station', { id: { type: Sequelize.INTEGER, primaryKey: true, allowNull: false}, name: { type: Sequelize.STRING, allowNull: false} }) var StationSnapshotEntity = sequelize.define('StationSnapshot', { id: { type: Sequelize.BIGINT, autoIncrement: true, primaryKey: true}, snapshotTimestamp: { type: Sequelize.BIGINT, allowNull: false} }) StationEntity.hasMany(StationSnapshotEntity, {as: 'Snapshots', foreignKeyConstraint: true, allowNull: false}) 我在sequelize.sync({force: true})之后得到以下日志: Executing: DROP […]