Tag: sequelize.js

续集多对多关联 – 未find方法

我有两个n:m sequelize模型,如下所示 // Organization Model module.exports = { attributes: { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, name: { type: Sequelize.STRING, required: true }, }, associations: function() { Organization.belongsToMany(Contact, { through : OrganizationContact, foreignKey: { name: 'organizationId', allowNull: false } }); } }; // OrganizationContact Model module.exports = { attributes: { id: { […]

如何使用Sequelize更新logging?

我正尝试使用Sequelize更新logging表格。 不幸的是,我试图更新的事件的ID似乎是未定义的。 我怎样才能正确地发送它? 我在我的控制器中的代码块如下所示: router.get('/edit/:eventid', function(req, res) { var eventid = req.params.eventid; Event.findById(req.params.eventid).then(function(event) { eventid= event.eventid; title= event.title; description= event.description; availabletickets=event.availabletickets; date= event.date; newDate= date.toString(); newdate= newDate.substring(0,21); console.log(eventid); }) .then(function() { res.render('eventEdit', { eventid: eventid, pagetitle: ' Edit Event', title: title, description:description, availabletickets:availabletickets, newdate: newdate, }); }) .catch(function(error) { res.render('error', { error: error }); }); […]

在查询中join限制和偏移不正确的位置

我在nodeJs中使用了sequelize,我有这样的代码: Time_Sheet_Details.findAll({ include: [ { model: timesheetNotesSubcon, required: false, attributes:["note","file_name", "id", "working_hrs", "timestamp", "has_screenshot", "notes_category"] }, { model: Timesheet, attributes:["id","leads_id","userid"], include:[ { model: Lead_Info, attributes:["id","fname","lname","email","hiring_coordinator_id","status"], where: { hiring_coordinator_id : 326}, include:[{ model: adminInfoSchema, required: false, attributes:["admin_id","admin_fname", "admin_lname", "admin_email", "signature_contact_nos", "signature_company"], }] }, {model:Personal_Info,attributes:["userid","fname","lname","email"]} ], }], where: { reference_date: filters.reference_date }, order:[ ["id","DESC"] ], offset:((1-1)*30), limit […]

使用Sequelize.jsjoin特定的列

我正在尝试使用Sequelize.js在特定列上join一些表格。 到目前为止,我的代码是这样的: table_1.findall({ include: [{ model: table_2 attributes: ['id', 'another_id'] include: [{ model: table_3 required: true attributes: ['time'] }] }] }) 每个表的主键都是“id”。 这似乎等同于下面的SQL(为了简洁起见,我显示了SELECT *,因为这不是这个问题的焦点): SELECT * FROM table_1 as t1 LEFT OUTER JOIN table_2 as t2 ON t1.id = t2.t1_id INNER JOIN table_3 as t3 ON t2.id = t3.t2_id 我想有这样的东西: SELECT * FROM table_1 as […]

在Node.js中使用SERIAL的外键违规

在继续使用cockroachdb示例为node.js使用sequelize orm构build应用程序时,我扩展了它以在模型之间添加关联。 主键是INT,通过unique_rowid()自动递增。 根据你的文档,这是你的SERIAL的实现。 同步我的模型后,我尝试使用以下代码创buildlogging: models.sequelize.sync({ force: true }) .then(function () { return models.Customer.create({ name: "Sample Customer" }) }) .then(function (result) { const id = parseInt(result.dataValues.id, 10) return models.Order.bulkCreate([ { subtotal: 100.00, customer_id: id }, { subtotal: 200.00, customer_id: id } ]) }) 当这运行,我得到“ error: foreign key violation: value [255737842446434300] not found in customers@primary [id] […]

在where子句中,不会接受有效的moment.js对象

我有以下的sequelize查询: let prms = periods.map((period, index, arr) => { if (arr[index + 1]) { return sequelize.models.Result.aggregate('value', 'avg', { where: { createdAt: { $lt: arr[index + 1], $gt: period }, type: hook.params.headers.type, entityId: hook.params.headers.entity } }).catch(err => console.log('err')); } }) 现在,where对象的createdAt属性导致了我这个问题: 错误:错误:在Object.escape(C:\ Users \ George \ Source \ Repos \ myProj \ node_modules \ sequelize \ […]

返回值调用方法

我为我的ExpressJS项目使用Sequelize。 我在我的模型中有这个代码: exports.findAll = () => { let data = []; Book.findAll().then(books => { data = books; // book has array of objects }); return data; // but data doesn't have even after assigning books to data }; 我想方法findAll返回值由BookController调用时 。

sequelize原始查询错误:查询不是一个函数

我试图在快速应用程序中使用来自sequelize的原始查询。 我的文件夹结构是: / /index.js /models/index.js /models/price.js /controllers/price.js 我想使用sequelize,我已经在控制器的/models/index.js中定义了。 这是/models/index.js: "use strict"; var fs = require("fs"); var path = require("path"); var Sequelize = require('sequelize') , sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, { dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb' port: 3306, // or 5432 (for postgres) timezone:'America/Sao_Paulo', }); sequelize .authenticate() .then(function(err) { console.log('Connection has been established […]

续集关联

我试图使用sequelizejs 1.4,并阅读文档后,仍然混淆关联。 var User = sequelize.define('User',{/ * … /})var Project = sequelize.define('Project',{/ … * /}) Project.hasOne(用户) 每次都必须包含这些代码吗? 我试图将模型分解为单个文件,但是看着这个让我觉得我需要在项目模型中定义用户。 或者,我是否需要将模型关联到更高层次? p1 = new Project()u1 = new User() p1.hasOne(U1) 只是对我来说不正确。 我知道我必须错过一些明显的东西,只需要有人拉开开灯的开关:) 谢谢

对Sequelize协会感到困惑

我刚刚发现Sequelize是一个很好的ORM框架,可以在我的节点+ MySQL webapp中使用。 但是当我devise我的数据库表时,我对Sequelize关联感到困惑。 只要想一想,webapp中的用户和系统通知就是一个非常简单的例子: 该网站有许多用户 该网站会向一些或每个用户发送通知,并且相同的通知只保存为一条logging 用户可以知道他/她收到的任何通知是否被读取(通过读取date) 然后我devise了3个表格: User : id , name Notification : id , title , content , sendAt Notify : id , UserId (作为接收者), NotificationId , readAt 我可以简单地通过Sequelize定义User和Notification模型。 但是我只是不知道如何使用关联使Notify与这两个表相关,通过外键UserId和NotificationId 。 我试图使用这样的hasOne关联: Notify.hasOne(User); Notify.hasOne(Notification); 然后在User和Notification表中都有一个NotifyId列。 这不是我的期望。 也许我认为使用关联的方式是错误的,所以我想知道如何正确使用它? 另外,如果我想以JSON的forms获得结果: [ {id: 123, user: [Object some user], notification: [Object some notification], readAt: null}, […]