Tag: sequelize.js

从一个表中获取所有行的列表,满足两个不同表中的多个条件

我有4个表格 – employee , company , employee_company company和action 。 employee_company包含employee_id和company_id ,其中action包含employee_id 。 或者, employee_company对employee和company都有一个外键,而对employee有一个外键。 我试图想出一个方法来获取属于拥有多名员工的公司的所有员工的列表,并且该公司的任何员工都没有在action表中的条目。 我使用sequelize.js作为orm,所以无论是原始SQL还是通过sequelize实现的一些方法都是有帮助的。

使用子select装订JS自定义查询

我是ORM的新手,我不知道如何使用ORM来做到这一点。 考虑到我有5个表格: 标签,拍卖,用户,user_tag和auction_tag 我正在使用SequelizeJS,我怎样才能得到这样的查询? select A.id, A.title, A.content, A.createdAt, U.id, U.picture, U.name from Auction A, User U, User U2 where A.userId = U.id and U2.id = x and exists (select AT.id from Auction_Tag AT where AT.auction = A.id and AT.tag in ( select T.id from Tag T, User_Tag UT where U2.id = UT.user and UT.tag […]

如何使用Sequelize.js和PostgreSQLvalidation数组字段的数组input?

我使用Node.js + PostgreSQL + Sequelize.js 2.0.2 。 我有以下模式定义的电话字段可能是string数组,但只允许数字: var User = sequelize.define("User", { …. …. /** Phone number * Multiple numbers should be registered due to */ phone: { type: Sequelize.ARRAY(Sequelize.STRING), allowNull: false, unique: true, validate: { isNumeric: true } }, …. …. }); 数字电话号码数组未通过validation。 input : [ '9252522525', '9252525555' ] 错误 : { [SequelizeValidationError: […]

如何在相同的查询中使用AND和OR运算符进行后续处理?

我执行下面的查询,但我给错误。 我想结束我发布的SQL查询的结果。 userServiceAppointmentModel.findAll({ where: { technician_id: resultsFromAuthentication.technician_id, is_confirmed_by_user: 1, $or: { service_start_time: { gte: curLocalDate }, service_running_status: 1 } }, attributes: attributes }).complete(function (err, appointmentResponse) { if (err) { console.log(err); } SELECT `id`, `technician_id`, `user_id`, `service_id`, `service_name`, `service_location_string`, `service_location_latitude`, `service_location_longitude`, `service_start_time`, `service_end_time`, `notes`, `total_cost`, `service_cost`, `is_confirmed_by_user`, `is_confirmed_by_technician`, `service_running_status`, `service_start_time_by_technician`,`service_complete_time_by_technician` FROM `user_service_appointment` AS `user_service_appointment` WHERE `user_service_appointment`.`technician_id`=154 […]

节省许多许多

我徘徊,如果有任何扩展教程如何保存多对多的关系? 我发现文档比基本更多。 它缺less许多使用案例的例子。 我有两种模式:客户端和规则。 他们有n:n的关系。 客户: var Client = sequelize.define('client', { title: { type: DataTypes.STRING(200), allowNull: false }, company: { type: DataTypes.STRING(200), allowNull: false }, vendor: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false }, consumer: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true }, address_id: { type: DataTypes.INTEGER, allowNull: true } },{ paranoid: true, underscored: true, […]

继续保存与之相关的多个对象

我不知道该怎么称呼这个问题,但这是我的设置: var AcademyModule = sequelize.define('academy_module', { academy_id: { type: DataTypes.INTEGER, primaryKey: true }, module_id: { type: DataTypes.INTEGER, primaryKey: true }, module_module_type_id: DataTypes.INTEGER, sort_number: DataTypes.INTEGER, requirements_id: DataTypes.INTEGER }, {freezeTableName: true,} 通过以下assosiation : Requirements = sequelize.define('requirements', { id: DataTypes.INTEGER, value: DataTypes.STRING, requirement_type_id: DataTypes.INTEGER }, {freezeTableName: true}); AcademyModule.belongsTo(Requirements, {foreignKey: 'requirements_id'}); 现在,你可以从我的表设置看到,我将不得不保存在requirements表中的一行,然后使用插入的id插入到academy_module表。 为此,我创build了以下内容: add: function (requirements,academyModule,onSuccess, onError) { var […]

无法使用Sequelizer for NodeJS为连接表添加where子句

我正在执行以下的地方。 引起我的问​​题的关键逻辑是在#7行,我试图指定一个连接表上的where条件。 models.Portfolio.findAll({ include: [{ model: models.PortfolioPermissions, }], where: models.sequelize.or( {'userId': userId}, {'PortfolioPermissions.userId': userId} ), order: [['startDate', 'DESC']] }) 您可以看到下面的结果查询在第9行有一个主要问题。 Sequelize正在将我的where子句与投资组合表混为一谈。 SELECT `portfolios`.*, `sharedUsers`.`id` AS `sharedUsers.id`, `sharedUsers`.`permissions` AS `sharedUsers.permissions`, `sharedUsers`.`userId` AS `sharedUsers.userId`, `sharedUsers`.`portfolioId` AS `sharedUsers.portfolioId` FROM `portfolios` LEFT OUTER JOIN `portfolioPermissions` AS `sharedUsers` ON `portfolios`.`id` = `sharedUsers`.`portfolioId` WHERE (`portfolios`.`userId`=1 OR `portfolios`.`PortfolioPermissions.userId`=1) ORDER BY `startDate` DESC; […]

使用Sequelize.js标记或检查字段

在Sequelize模型中是否有一种很好的方式来“标记”或“审查”一组属性? 我的意思是类似nodejs-model支持的标记。 例如,我的用户架构有一个密码字段。 我不希望这从任何API调用返回。 我也不想在用户控制器中审查这个,因为在每个路由的基础上configuration审查器似乎是不好的做法。 我很想能够做一些像User.find({query}, {tag: 'public')

在Sequelize中创build关联进行左连接

我想使用Sequelize做一个左连接,它和这个SQL代码一样。 SELECT * FROM events LEFT JOIN zones ON event_tz=zid WHERE event_creator_fk=116; 我有两个表: events和zones (与时区列表)。 当查询特定用户创build的所有事件时,我也想获取TZ的时区​​名称和其他详细信息。 我已经尝试了许多解决scheme的组合,通过查看示例代码,其他堆栈溢出问题和尽可能最好的文档。 查询总是有效的,但不做任何连接。 也就是说,它下面的代码总是返回事件列表,但没有来自zones表的时区数据。 生成的SQL是正确的,除了它没有…LEFT JOIN zones ON event_tz=zid…部分。 下面的代码是错误的。 详情请参阅答案。 db.Event.findAll( { where: { event_creator_fk: someUserID } }, { include: [{ model: db.Zone } ]} ); 如果我理解正确,在表中添加关联在sequelize结果在自动创build一个额外的列。 这不是我想要做的。 我不希望Sequelize以任何方式修改表或数据库。 我想设置我的数据库,它没有Sequelize的表。 因此,我不调用sequelize.sync() 。 我不知道是否有按照我想要的方式build立关联。 模型定义 module.exports = function (sequelize, DataTypes) […]

Node.js的后续关联包括

这是一个错误,当我查询模型(短版本): var User = db.define('User', { login: Sequelize.STRING(16), password: Sequelize.STRING, }); var Group = db.define('Group', { name: Sequelize.STRING, }); var GroupSection = db.define('GroupSection', { name: Sequelize.STRING, }); Group.belongsTo(GroupSection, { as: 'GroupSection', foreignKey: 'GroupSectionId' }); GroupSection.hasMany(Group, { as: 'Groups', foreignKey: 'GroupSectionId' }); Group.belongsTo(Group, { as: 'ParentGroup', foreignKey: 'ParentGroupId' }); Group.hasMany(Group, { as: 'ChildGroups', foreignKey: 'ParentGroupId' }); […]