Model definition

To define mappings between a model and a table, use the define method. Each column must have a datatype, see more about datatypes.

  1. class Project extends Model {}
  2. Project.init({
  3. title: Sequelize.STRING,
  4. description: Sequelize.TEXT
  5. }, { sequelize, modelName: 'project' });
  6. class Task extends Model {}
  7. Task.init({
  8. title: Sequelize.STRING,
  9. description: Sequelize.TEXT,
  10. deadline: Sequelize.DATE
  11. }, { sequelize, modelName: 'task' })

Apart from datatypes, there are plenty of options that you can set on each column.

  1. class Foo extends Model {}
  2. Foo.init({
  3. // instantiating will automatically set the flag to true if not set
  4. flag: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true },
  5. // default values for dates => current time
  6. myDate: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
  7. // setting allowNull to false will add NOT NULL to the column, which means an error will be
  8. // thrown from the DB when the query is executed if the column is null. If you want to check that a value
  9. // is not null before querying the DB, look at the validations section below.
  10. title: { type: Sequelize.STRING, allowNull: false },
  11. // Creating two objects with the same value will throw an error. The unique property can be either a
  12. // boolean, or a string. If you provide the same string for multiple columns, they will form a
  13. // composite unique key.
  14. uniqueOne: { type: Sequelize.STRING, unique: 'compositeIndex' },
  15. uniqueTwo: { type: Sequelize.INTEGER, unique: 'compositeIndex' },
  16. // The unique property is simply a shorthand to create a unique constraint.
  17. someUnique: { type: Sequelize.STRING, unique: true },
  18. // It's exactly the same as creating the index in the model's options.
  19. { someUnique: { type: Sequelize.STRING } },
  20. { indexes: [ { unique: true, fields: [ 'someUnique' ] } ] },
  21. // Go on reading for further information about primary keys
  22. identifier: { type: Sequelize.STRING, primaryKey: true },
  23. // autoIncrement can be used to create auto_incrementing integer columns
  24. incrementMe: { type: Sequelize.INTEGER, autoIncrement: true },
  25. // You can specify a custom column name via the 'field' attribute:
  26. fieldWithUnderscores: { type: Sequelize.STRING, field: 'field_with_underscores' },
  27. // It is possible to create foreign keys:
  28. bar_id: {
  29. type: Sequelize.INTEGER,
  30. references: {
  31. // This is a reference to another model
  32. model: Bar,
  33. // This is the column name of the referenced model
  34. key: 'id',
  35. // This declares when to check the foreign key constraint. PostgreSQL only.
  36. deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE
  37. }
  38. },
  39. // It is possible to add comments on columns for MySQL, PostgreSQL and MSSQL only
  40. commentMe: {
  41. type: Sequelize.INTEGER,
  42. comment: 'This is a column name that has a comment'
  43. }
  44. }, {
  45. sequelize,
  46. modelName: 'foo'
  47. });

The comment option can also be used on a table, see model configuration.