sequelize.js

Accessing belongsToMany associates inside of instance

烈酒焚心 提交于 2019-12-24 18:56:21
问题 I am in the process of converting an existing rails app to nodejs using sequelize to interface to the existing database (created using ActiveRecord). This is a shopping application, so I have a Product class and a Cart class that I want to associate together. In my existing application I use a third class, CartLineItem to associate them together. Inside of the Product class I use the following: classMethods: { associate: function(models) { Cart.belongsToMany(models.Product, { through: models

Change sequelize timezone

大城市里の小女人 提交于 2019-12-24 18:44:32
问题 I want to make restful app in nodejs Server: centos 7 64x Database: postgresql Additional: express, sequelize Table: datetime with timezone When I selecting rows with sequelize from database, created_at column gives me wrong time. 5 hour added to datetime. I change timezone configuration of centos to +5 (Tashkent/Asia) Also change postgresql timezone configuration to +5 Datetime is correct in database when shows. But when I select it converts to like this "createdAt": "2018-08-12T17:57:20

Sequelize - How to get entries from one table that are not assiciated by another table

拈花ヽ惹草 提交于 2019-12-24 17:18:35
问题 I used Sequelize to define three Entities for users rating posts: var User = sequelize.define('User', { email: { type: Sequelize.STRING, primaryKey: true } }); var Post = sequelize.define('Post', { link: { type: Sequelize.STRING, primaryKey: true } }); var Rating = sequelize.define('Rating', { result: Sequelize.STRING }); Rating.belongsTo(Post); Post.hasMany(Rating); Rating.belongsTo(User); User.hasMany(Rating); A user can rate several posts. Each rating belongs to exactly one user and one

Paginate with Sequelize.js at SQL 2008 (that does not support FETCH)

牧云@^-^@ 提交于 2019-12-24 15:27:19
问题 So, I'm using sequelize.js with tedious for mssql support. MSSQL = SQL Server 2008 Well, So there is this big table named Clientes. I have to fetch data from it, but it is quite huge so I'll have to paginate trhough I tried the 'right way', as in the docs: app.use('/clientes', function(req, res){ var page = req.params.page || 0; sequelize.Clientes.findAll({limit: 30, offset: 5 }) .then(function(result) { res.json({result: result}); }); }); It should work, BUT it uses a query that is something

Bulk postgres insert where timestamp has null constraint set through sequalize

穿精又带淫゛_ 提交于 2019-12-24 14:06:12
问题 I am using Postgres and Sequelize with my node app. I have set up the models and allows Sequalize to produce the tables. I now want to move the data across using something like this. insert into archive (id,title) select id, title from old_archives; However, I am getting null errors as Sequelize uses createdAt and updatedAt columns. Is there a way around this? Error is ERROR: null value in column "createdAt" violates not-null constraint DETAIL: Failing row contains (2, null, Dewerstone Rocks,

Sequelize.js how to map a varbinary type from MySQL

不问归期 提交于 2019-12-24 13:46:47
问题 I am trying to use Sequelize.js to map all of the columns in my MySQL table. The mysql table "User" has a Password column as type varbinary(50) . Does Sequelize.js support mapping for a varbinary type? I did not see such an option in the Sequelize docs, is there another way I can map it? 回答1: The built-in types in sequelize just maps to strings, so intead of: User = sequelize.define('user', { Password: Sequelize.STRING }); You can write your own string like this: User = sequelize.define('user

How Can I set the default Sort order in FeathersJs

ε祈祈猫儿з 提交于 2019-12-24 10:55:11
问题 I have an issue with Feathersjs , integrating with sequalize. If I set the default pagination like below, and there is no sort specified it will generate an error because the SQL statement generated is invalid. Service Created with default of 5: app.use('/manifests', service({ paginate: { default: 5, max: 25 } })); SQL Statement Generated ( setting a limit of 20 ) SELECT [id], ...etc FROM [Manifest] AS [Manifest] OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY It Makes sense to set a default order ,

Sequelize limit and order not work with condition query

旧时模样 提交于 2019-12-24 08:47:43
问题 I am building a conditional ORM query let defaultWhere ={ isPublic:1 }; //query to build let query = { distinct:true, include:[ { model:DealsMeta, where:{ type:'view' }, as: "totalviews", attributes: ['id'], required:false, }, { model:DealsTransactions, as: "totaltransactions", attributes: ['id'], required:false } ] }; let keywordWhere = {}; //add the search parameter if(params.keyword!=undefined&&params.keyword!=""){ keywordWhere = { [Op.or]:[ {title: { $like: '%' + params.keyword + '%' }},

Query self-join with Sequelize, including related record

十年热恋 提交于 2019-12-24 08:35:08
问题 We're using Postgres for a Node.js app and have a Sequelize model Entry which is roughly defined as: const entriesModel = sequelize.define('Entry', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, post_date: { type: DataTypes.DATE, allowNull: false, defaultValue: () => new Date() } /* ...more fields here, etc, etc... */ }, { classMethods: { associate: (models) => { entriesModel.hasOne(models.Entry, { onDelete: 'CASCADE', foreignKey: { name: 'parent_id', allowNull:

Sequelize belongsTo not adding foreign key constraints to database table

纵饮孤独 提交于 2019-12-24 08:08:38
问题 I am using sequelize ORM for my node.js application and have created two simple models. company.model.js location.model.js A company can have multiple locations and a location can belongs to one company. Here are my models: company.model.js 'use strict'; module.exports = function(sequelize, DataTypes) { var Company = sequelize.define('Company', { id: { type: DataTypes.INTEGER(3), allowNull: false, primaryKey: true, autoIncrement: true, comment: "Primary and auto incremented key of the table"