PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 14, 2022

[FIXED] When creating an association with Sequelize and Migrations, is the alias name absolute or changed to AliasId?

 April 14, 2022     javascript, migration, postgresql, sequelize.js     No comments   

Issue

I have a sequelize association like so in the Housing model:

Housing.belongsTo(models.User, {as : Owner});

When adding the column to the model and Migrations file, by this association, is the table going to be named "Owner" exactly like the Alias, or "OwnerId", as it associates on the Id key of the User table?


Solution

It will be based on the key you used in the model definition for sequelize.define(). If you have a Model called Owner with a key value of owner then when you call Housing.belongsTo() it will add a column to the Housing model called ownerId. This referential column is used to query for the related rows which you can do using the generated helper methods like house.getOwner() - where house is a Model Instance.

If you have defined underscored: true for your model definition then the generated column will be owner_id.



Answered By - doublesharp
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing