Tag: sequelize.js

在Sequelize.js中“belongsTo”vs“hasMany”

B.belongsTo(A)与A.hasMany(B)之间有什么区别? Artist = sequelize.define('Artist', {}); Album = sequelize.define('Albums', {}); Album.belongsTo(Artist, foreignKey: 'album_belongsl_artist'); Artist.hasMany(Album, foreignKey: 'artist_hasmany_albums'); 如果它在两种情况下创buildAlbum的依赖表?

如何使用Sequelize模型自动生成Sequelize CLI迁移?

我有一套Sequelize模型。 我想使用迁移,而不是数据库同步。 Sequelize CLI似乎能够做到这一点,根据这篇文章 :“当您使用CLI生成模型时,您也将获得免费的迁移脚本。” 如何从现有的Sequelize模型中使用Sequelize CLI自动生成迁移?

sequelize js – 限制和sorting错误

在sequelize js中对查询进行sorting的正确方法是什么? 例: db.model.findAll({ where: conditions, order: 'postDate DESC', limit: 10, offset: 0, include: [model1, model2] }).complete(function(err, results){console.log(results); }); 导致限制和抵消的结果,首先,然后它进行sorting。 在限制结果之前,我应该怎么做sorting呢? 在未来谁会遇到这个bug,这是修复 order: [["postDate","DESC"]]

autoIncrement如何在NodeJs的Sequelize中工作?

Sequelize的文档没有提到autoIncrement 。 它只包括下面的例子: // autoIncrement can be used to create auto_incrementing integer columns incrementMe: { type: Sequelize.INTEGER, autoIncrement: true } 基于这个例子,我有以下代码: db.define('Entries', { id: { type: Seq.INTEGER, autoIncrement: true, primaryKey: true }, title: { type: Seq.STRING, allowNull: false }, entry: { type: Seq.TEXT, allowNull: false } } 现在,当我想创build一个条目,我想要做这样的事情: models.Entry.create({title:'first entry', entry:'yada yada yada'}) 但是,当我执行该代码时,我得到一个数据库错误: 列“id”中的空值违反了非null约束 […]

根据关联进行search查找

我将如何使用Sequelize来查找关系中的列满足条件的所有人? 一个例子是find作者姓氏“希区柯克”的所有书籍。 书籍模式包含与作者表的hasOne关系。 编辑:我明白这可以做一个原始的SQL查询,但寻找另一种方法

想要了解NodeJS应用程序结构(Full JavaScript Stack)

我想知道一个典型的NodeJS应用程序的结构,因为我阅读和看到的项目越多,我就越困惑,特别是针对这样的问题:(甚至当我更新这个问题时)。 以MEAN堆栈为例,据我所知,NodeJS和Express负责服务器 部分,提供服务器接口等。MongoDB和Angular相当简单。 但是业务逻辑应该去哪里? 说如果我有一个controller.js包含一个函数, 和route.js文件绑定请求与这个控制器function。 我的问题是在哪个模块下这些文件属于/运行(Express或NodeJS?) NodeJS应用的起点在哪里? 说index.php是一个PHP应用程序的起点,但它在哪里NodeJS应用程序? 我可以看到所有nodejs项目都有一个名为server.js或app.js等的文件(包含诸如module.exports = app;类的东西)但是module.exports = app;如何知道要find并执行它的文件呢? 我是NodeJS上的一个新鲜的noob,express,sequelize.js / mongoose,jade / ejs,但是想要开始一个nodejs项目。 请大家详细说明每个模块提供的实际function,以及一个完整的js堆叠nodejs应用程序的典型结构的一般介绍? 提前致谢!

Sequelize:如何从现有数据库导入定义

即使我正在使用现有的数据库,我是否需要为Sequelize手写模型定义。 如果不是必需的,那么如何在现有的数据库中使用Sequelize呢? 我已经在Doctrine中定义了数据库的模式,所以我不必再写一组模型定义。

如何使用包含具有后续属性的属性?

任何想法如何使用包含属性(当你只需要包括包含表的特定字段)与sequ​​elize? 目前我有这个(但不能按预期工作): var attributes = ['id', 'name', 'bar.version', ['bar.last_modified', 'changed']]; foo.findAll({ where : where, attributes : attributes, include : [bar] }).success(function (result) { …

如何在nodejs中使用sequelize进行连接查询

我正在使用序列化ORM; 一切都很好,干净,但我有一个问题,当我使用join查询。 我有两个模型:用户和post。 var User = db.seq.define('User',{ username: { type: db.Sequelize.STRING}, email: { type: db.Sequelize.STRING}, password: { type: db.Sequelize.STRING}, sex : { type: db.Sequelize.INTEGER}, day_birth: { type: db.Sequelize.INTEGER}, month_birth: { type: db.Sequelize.INTEGER}, year_birth: { type: db.Sequelize.INTEGER} }); User.sync().success(function(){ console.log("table created") }).error(function(error){ console.log(err); }) var Post = db.seq.define("Post",{ body: { type: db.Sequelize.TEXT }, user_id: { type: […]

Heroku + Node:找不到模块错误

我的Node应用程序在本地运行良好,但在部署到Heroku时出现错误。 该应用程序在/models文件夹中使用Sequelize,其中包含index.js , Company.js和Users.js 。 在本地,我可以使用/models/index.js的以下代码导入模型: // load models var models = [ 'Company', 'User' ]; models.forEach(function(model) { module.exports[model] = sequelize.import(__dirname + '/' + model); }); 这工作正常,但是,当我部署到Heroku的应用程序崩溃与以下错误: Error: Cannot find module '/app/models/Company' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at module.exports.Sequelize.import (/app/node_modules/sequelize/lib/sequelize.js:219:24) at module.exports.sequelize (/app/models/index.js:60:43) at Array.forEach (native) at Object.<anonymous> […]