Basic Concepts

Source & Target

Let's first begin with a basic concept that you will see used in most associations, source and target model. Suppose you are trying to add an association between two Models. Here we are adding a hasOne association between User and Project.

  1. class User extends Model {}
  2. User.init({
  3. name: Sequelize.STRING,
  4. email: Sequelize.STRING
  5. }, {
  6. sequelize,
  7. modelName: 'user'
  8. });
  9. class Project extends Model {}
  10. Project.init({
  11. name: Sequelize.STRING
  12. }, {
  13. sequelize,
  14. modelName: 'project'
  15. });
  16. User.hasOne(Project);

User model (the model that the function is being invoked on) is the source. Project model (the model being passed as an argument) is the target.

Foreign Keys

When you create associations between your models in sequelize, foreign key references with constraints will automatically be created. The setup below:

  1. class Task extends Model {}
  2. Task.init({ title: Sequelize.STRING }, { sequelize, modelName: 'task' });
  3. class User extends Model {}
  4. User.init({ username: Sequelize.STRING }, { sequelize, modelName: 'user' });
  5. User.hasMany(Task); // Will add userId to Task model
  6. Task.belongsTo(User); // Will also add userId to Task model

Will generate the following SQL:

  1. CREATE TABLE IF NOT EXISTS "users" (
  2. "id" SERIAL,
  3. "username" VARCHAR(255),
  4. "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  5. "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  6. PRIMARY KEY ("id")
  7. );
  8. CREATE TABLE IF NOT EXISTS "tasks" (
  9. "id" SERIAL,
  10. "title" VARCHAR(255),
  11. "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  12. "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  13. "userId" INTEGER REFERENCES "users" ("id") ON DELETE
  14. SET
  15. NULL ON UPDATE CASCADE,
  16. PRIMARY KEY ("id")
  17. );

The relation between tasks and users model injects the userId foreign key on tasks table, and marks it as a reference to the users table. By default userId will be set to NULL if the referenced user is deleted, and updated if the id of the userId updated. These options can be overridden by passing onUpdate and onDelete options to the association calls. The validation options are RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL.

For 1:1 and 1:m associations the default option is SET NULL for deletion, and CASCADE for updates. For n:m, the default for both is CASCADE. This means, that if you delete or update a row from one side of an n:m association, all the rows in the join table referencing that row will also be deleted or updated.

underscored option

Sequelize allow setting underscored option for Model. When true this option will set thefield option on all attributes to the underscored version of its name. This also applies toforeign keys generated by associations.

Let's modify last example to use underscored option.

  1. class Task extends Model {}
  2. Task.init({
  3. title: Sequelize.STRING
  4. }, {
  5. underscored: true,
  6. sequelize,
  7. modelName: 'task'
  8. });
  9. class User extends Model {}
  10. User.init({
  11. username: Sequelize.STRING
  12. }, {
  13. underscored: true,
  14. sequelize,
  15. modelName: 'user'
  16. });
  17. // Will add userId to Task model, but field will be set to `user_id`
  18. // This means column name will be `user_id`
  19. User.hasMany(Task);
  20. // Will also add userId to Task model, but field will be set to `user_id`
  21. // This means column name will be `user_id`
  22. Task.belongsTo(User);

Will generate the following SQL:

  1. CREATE TABLE IF NOT EXISTS "users" (
  2. "id" SERIAL,
  3. "username" VARCHAR(255),
  4. "created_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  5. "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  6. PRIMARY KEY ("id")
  7. );
  8. CREATE TABLE IF NOT EXISTS "tasks" (
  9. "id" SERIAL,
  10. "title" VARCHAR(255),
  11. "created_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  12. "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  13. "user_id" INTEGER REFERENCES "users" ("id") ON DELETE
  14. SET
  15. NULL ON UPDATE CASCADE,
  16. PRIMARY KEY ("id")
  17. );

With the underscored option attributes injected to model are still camel cased but field option is set to their underscored version.

Cyclic dependencies & Disabling constraints

Adding constraints between tables means that tables must be created in the database in a certain order, when using sequelize.sync. If Task has a reference to User, the users table must be created before the tasks table can be created. This can sometimes lead to circular references, where sequelize cannot find an order in which to sync. Imagine a scenario of documents and versions. A document can have multiple versions, and for convenience, a document has a reference to its current version.

  1. class Document extends Model {}
  2. Document.init({
  3. author: Sequelize.STRING
  4. }, { sequelize, modelName: 'document' });
  5. class Version extends Model {}
  6. Version.init({
  7. timestamp: Sequelize.DATE
  8. }, { sequelize, modelName: 'version' });
  9. Document.hasMany(Version); // This adds documentId attribute to version
  10. Document.belongsTo(Version, {
  11. as: 'Current',
  12. foreignKey: 'currentVersionId'
  13. }); // This adds currentVersionId attribute to document

However, the code above will result in the following error: Cyclic dependency found. documents is dependent of itself. Dependency chain: documents -> versions => documents.

In order to alleviate that, we can pass constraints: false to one of the associations:

  1. Document.hasMany(Version);
  2. Document.belongsTo(Version, {
  3. as: 'Current',
  4. foreignKey: 'currentVersionId',
  5. constraints: false
  6. });

Which will allow us to sync the tables correctly:

  1. CREATE TABLE IF NOT EXISTS "documents" (
  2. "id" SERIAL,
  3. "author" VARCHAR(255),
  4. "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  5. "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  6. "currentVersionId" INTEGER,
  7. PRIMARY KEY ("id")
  8. );
  9. CREATE TABLE IF NOT EXISTS "versions" (
  10. "id" SERIAL,
  11. "timestamp" TIMESTAMP WITH TIME ZONE,
  12. "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  13. "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
  14. "documentId" INTEGER REFERENCES "documents" ("id") ON DELETE
  15. SET
  16. NULL ON UPDATE CASCADE,
  17. PRIMARY KEY ("id")
  18. );

Enforcing a foreign key reference without constraints

Sometimes you may want to reference another table, without adding any constraints, or associations. In that case you can manually add the reference attributes to your schema definition, and mark the relations between them.

  1. class Trainer extends Model {}
  2. Trainer.init({
  3. firstName: Sequelize.STRING,
  4. lastName: Sequelize.STRING
  5. }, { sequelize, modelName: 'trainer' });
  6. // Series will have a trainerId = Trainer.id foreign reference key
  7. // after we call Trainer.hasMany(series)
  8. class Series extends Model {}
  9. Series.init({
  10. title: Sequelize.STRING,
  11. subTitle: Sequelize.STRING,
  12. description: Sequelize.TEXT,
  13. // Set FK relationship (hasMany) with `Trainer`
  14. trainerId: {
  15. type: Sequelize.INTEGER,
  16. references: {
  17. model: Trainer,
  18. key: 'id'
  19. }
  20. }
  21. }, { sequelize, modelName: 'series' });
  22. // Video will have seriesId = Series.id foreign reference key
  23. // after we call Series.hasOne(Video)
  24. class Video extends Model {}
  25. Video.init({
  26. title: Sequelize.STRING,
  27. sequence: Sequelize.INTEGER,
  28. description: Sequelize.TEXT,
  29. // set relationship (hasOne) with `Series`
  30. seriesId: {
  31. type: Sequelize.INTEGER,
  32. references: {
  33. model: Series, // Can be both a string representing the table name or a Sequelize model
  34. key: 'id'
  35. }
  36. }
  37. }, { sequelize, modelName: 'video' });
  38. Series.hasOne(Video);
  39. Trainer.hasMany(Series);