sequelize

Sequelize upsert

匿名 (未验证) 提交于 2019-12-03 02:51:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: i need to get the id for the inserted/updated record when using .upsert() in sequelize. right now .upsert() returns a boolean indicating whether the row was created or updated. return db . VenueAddress . upsert ({ addressId : address . addressId , venueId : venue . venueId , street : address . street , zipCode : address . zipCode , venueAddressDeletedAt : null }). then ( function ( test ){ //test returned here as true or false how can i get the inserted id here so i can insert data in other tables using this new id? }); 回答1: I

Prevent Sequelize from outputting SQL to the console on execution of query?

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a function to retrieve a user's profile. app.get('/api/user/profile', function (request, response) { // Create the default error container var error = new Error(); var User = db.User; User.find({ where: { emailAddress: request.user.username} }).then(function(user) { if(!user) { error.status = 500; error.message = "ERROR_INVALID_USER"; error.code = 301; return next(error); } // Build the profile from the user object profile = { "firstName": user.firstName, "lastName": user.lastName, "emailAddress": user.emailAddress } response.status

get .findOrCreate() error

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using Sequelize as ORM. Here's my user model: ### User model ### User = exports.User = globals.sequelize.define "User", username: globals.Sequelize.STRING email: type: globals.Sequelize.STRING validate: isEmail: true hash: globals.Sequelize.STRING salt: globals.Sequelize.STRING(512) fname: globals.Sequelize.STRING lname: globals.Sequelize.STRING country: globals.Sequelize.STRING I'm saving user: globals.models.User.findOrCreate username: "johny" password: "pass" email: "johny93[###]example.com" .success (user, created)-> console.log user

Sequelize Unknown column '*.createdAt' in 'field list'

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm getting a Unknown column 'userDetails.createdAt' in 'field list' When trying to fetch with association. Using findAll without association works fine. My code is as follows: var userDetails = sequelize.define('userDetails', { userId :Sequelize.INTEGER, firstName : Sequelize.STRING, lastName : Sequelize.STRING, birthday : Sequelize.DATE }); var user = sequelize.define('user', { email: Sequelize.STRING, password: Sequelize.STRING }); user.hasOne(userDetails, {foreignKey: 'userId'}); user.findAll({include: [userDetails] }).success(function

Sequelize: Using Multiple Databases

匿名 (未验证) 提交于 2019-12-03 02:00:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Do I need to create multiple instances of Sequelize if I want to use two databases? That is, two databases on the same machine. If not, what's the proper way to do this? It seems like overkill to have to connect twice to use two databases, to me. So for example, I have different databases for different functions, for example, let's say I have customer data in one database, and statistical data in another. So in MySQL: MySQL [ customers ]> show databases ; +--------------------+ | Database | +--------------------+ | customers | |

Sequelize belongsToMany additional attributes in join table

匿名 (未验证) 提交于 2019-12-03 01:36:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having problem with an additional attribute in the join table of the belongsToMany relation. In the set or add method this attribute is not being passed to mysql. I'm following the documentation pass as "through" the attribute within the set method, but it is not working. Would anyone know what could be wrong since following the documentation is not working? Note: The registration and update of the join is correct, only the additional attribute that is not being passed to the table. Functionality Model: export default function(sequelize,

How to call a MSSQL Stored Procedure using Sequelize?

匿名 (未验证) 提交于 2019-12-03 01:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am struggling with calling a MS SQL Stored Proc using Sequelize. This is how i normally call the stored proc from SSMS USE [MYDB] GO DECLARE @return_value int EXEC @return_value = [dbo].[GetThings_ByLocation] @BeginDate = N'2016-06-23', @EndDate = N'2016-07-09', @LocationID = NULL SELECT 'Return Value' = @return_value GO How would i make this call using sequelize? 回答1: Sequelize uses npm package Tedious ( https://www.npmjs.com/package/tedious ) to work with MS SQL. Connecting to the database is the same as others. You can use raw query to

Sequelize Issue By Separating Model Logic from Model Configuration

匿名 (未验证) 提交于 2019-12-03 01:26:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am following the sequelize best practices by connecting to a host and importing all models into one file and then calling that file when interacting the model. For some reason it looks like this is causing an issue as I am getting an error at using the define method for the sequelize variable and I ran a test with a file that contained both logic together and I was able to add a user. Error: TypeError: Cannot read property 'define' of undefined at new module.exports (/Users/user/Desktop/Projects/node/ann/app/models/ann-model.js:3:27) at

Node.js 7 how to use sequelize transaction with async / await?

匿名 (未验证) 提交于 2019-12-03 01:14:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Node.js 7 already support async / await syntax, in the sequelize transaction if the use of async / await will cause the transaction is not enabled, how should I use? 回答1: let transaction; try { // get transaction transaction = await sequelize.transaction(); // step 1 await Model.destroy({where: {id}, transaction}); // step 2 await Model.create({}, {transaction}); // commit await transaction.commit(); } catch (err) { // Rollback transaction if any errors were encountered await transaction.rollback(); } 回答2: The above code has an error in

How to make join querys using sequelize in nodejs

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am using sequelize ORM; everything is great and clean, but I had a problem when I use it with join queries. I have two models: users and posts. 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 ().