sequelize.js

Sequelize how to use association table?

萝らか妹 提交于 2020-04-12 05:00:59
问题 I'm having an issue with Sequelize because I don't know how to approach the problem. I have 3 tables : A (game), B(platform) and AB (game_platform). A can have 1 to many B and B can have 0 to many A. To do that I made an association table : AB. In Sequelize I created the A,B,AB models then I did : db.Game.belongsToMany(db.Platform, {as: 'Game', through: 'GamePlatformsJoin', foreignKey: 'game_platforms_fk_game'}); db.Platform.belongsToMany(db.Game, {as: 'Platform', through: 'GamePlatformsJoin'

Sequelize Composite Foreign Key

风流意气都作罢 提交于 2020-04-08 03:33:00
问题 I have a database with the following tables: CREATE TABLE IF NOT EXISTS `app_user` ( `user_id` INT NOT NULL, `user_name` VARCHAR(45) NOT NULL, PRIMARY KEY (`user_id`)) ENGINE = InnoDB; CREATE TABLE IF NOT EXISTS `user_folder` ( `user_id` INT NOT NULL, `document_id` INT NOT NULL, PRIMARY KEY (`user_id`, `document_id`), CONSTRAINT `fk_user_document_user` FOREIGN KEY (`user_id`) REFERENCES `zinc`.`app_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = InnoDB; CREATE TABLE IF NOT

Sequelize hasMany, belongsTo, or both?

馋奶兔 提交于 2020-03-18 12:29:48
问题 I want to properly setup one-to-one or one-to-many relationship with sequelize and as a matter of fact it all seems to be working just fine if i use either one of hasOne / hasMany or belongsTo in my model definition. For example the following associations do create the userId field on their Targets: User.hasMany(Email, { as: 'emails', foreignKey: 'userId', }) User.hasOne(Profile, { as: 'profile', foreignKey: 'userId', }) But almost everywhere in official docs i see something like: Projects

Sequelize — use UNIX timestamp for DATE fields

喜夏-厌秋 提交于 2020-03-18 05:46:12
问题 Is there a way to force Sequelize use UNIX Timestamp as default time format both for createdAt/updatedAt timestamps and for custom-defined Sequelize.DATE field types? Thanks! P.S. I'm using MySQL 回答1: At any given moment in time, there are two possible dates (depending on one's position relative to the international date line): that is, converting from a UNIX timestamp to a date requires one to consider the timezone. For example, the UNIX timestamp 946684800 is 2000-01-01 00:00:00Z . Whilst

How to use database connections pool in Sequelize.js

北城余情 提交于 2020-03-17 07:56:39
问题 I need some clarification about what the pool is and what it does. The docs say Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database. var sequelize = new Sequelize('database', 'username', 'password', { host: 'localhost', dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql', pool: { max: 5, min: 0, idle: 10000 }, // SQLite only storage: 'path/to/database.sqlite' }); 回答1: When your application needs to retrieve data from the

Specifying specific fields with Sequelize (NodeJS) instead of *

帅比萌擦擦* 提交于 2020-03-17 04:27:22
问题 Alright so I have a project in NodeJS where I'm utilizing Sequelize for a MySQL ORM. The thing works fantastically however I'm trying to figure out if there is a way to specify what fields are being returned on a query basis or if there's even a way just to do a .query() somewhere. For example in our user database there can be ridiculous amounts of records and columns. In this case I need to return three columns only so it would be faster to get just those columns. However, Sequelize just

sequelize foreign key returning as “Null”

心不动则不痛 提交于 2020-03-05 11:56:34
问题 I have added foreign keys to my project using sequelize. User is the parent table and profile_personals is the child table. When I post something the foreign key comes out as NULL. I'm trying to create it so when I post something through postman it can grab the User id Automatically. db.js file const db = {}; db.sequelize = sequelize; db.Sequelize = Sequelize; // Models db.Users = require('../model/User.js')(sequelize, Sequelize); db.Personal = require('../model/profile_personal.js')

sequelize foreign key returning as “Null”

你离开我真会死。 提交于 2020-03-05 11:55:12
问题 I have added foreign keys to my project using sequelize. User is the parent table and profile_personals is the child table. When I post something the foreign key comes out as NULL. I'm trying to create it so when I post something through postman it can grab the User id Automatically. db.js file const db = {}; db.sequelize = sequelize; db.Sequelize = Sequelize; // Models db.Users = require('../model/User.js')(sequelize, Sequelize); db.Personal = require('../model/profile_personal.js')

Sequelize not creating model association columns

我的梦境 提交于 2020-03-03 09:09:04
问题 I'm a little confused with when Sequelize creates the fields for associations. I've created my migrations using sequelize-cli. This generated a migration and model file. Then in the model file I populated my associations. Then ran npx sequelize-cli db:migrate . This creates the tables but not the foreign keys needed for the associations defined in the model. For example: migration-quesions: "use strict"; module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.createTable

Querying association tables in Sequelize

我只是一个虾纸丫 提交于 2020-02-28 09:31:28
问题 I have two tables ( users and games ) joined by an association table ( game_players ), creating a many-to-many relationship: models.Game.belongsToMany(models.User, { through: models.GamePlayer, as: 'players' }); models.User.belongsToMany(models.Game, { through: models.GamePlayer, foreignKey: 'user_id' }); In addition to the foreign keys user_id and game_id , game_players has a few extra columns for link-specific data: sequelize.define('game_player', { isReady: { defaultValue: false, type: