Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sequelize has many association lowercase in views

Everything works here. The only issue is that in my handlebars file, I have to get to a person's tasks like this:

this.Tasks

I would rather have it appear like this:

this.tasks

How would I customize Sequelize to do so?

This is what the root route of my app looks like (it's rendering the index.handlebars file)

project management app

my route:

router.get('/', function(req, res) {
  models.Person.findAll({
    include: [ models.Task ]
  }).then(function(people) {
    res.render('index', {
      user_id: req.session.user_id,
      email: req.session.user_email,
      logged_in: req.session.logged_in,
      people: people
    });
  });
});

my index.handlebars file:

  {{#each people}}
    <li>
      {{this.name}}
      {{#if ../logged_in}}
        <a href="/people/{{this.id}}/destroy"> destroy</a>
      {{/if}}
      <ul>
        {{#if ../logged_in}}
          <li>
            <form action="/people/{{this.id}}/tasks/create" method="POST" style="display: inline">
              <input type="text" name="task" placeholder="add task here">
              <input type="submit" value"assign task">
            </form>
          </li>
        {{/if}}

        {{#each this.Tasks }}
          <li>
            {{this.task}}
            {{#if ../../logged_in}}
              <a href="/people/{{this.person_id}}/tasks/{{this.id}}/destroy">destroy</a>
            {{/if}}
          </li>
        {{/each}}
      </ul>
    </li>
  {{/each}}

my people table migration:

"use strict";

module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface
      .createTable('people', {
        id: {
          type: Sequelize.INTEGER,
          autoIncrement: true,
          primaryKey: true
        },
        name: Sequelize.STRING,
        created_at: Sequelize.DATE,
        updated_at: Sequelize.DATE
      });
  },

  down: function(queryInterface, Sequelize) {
    return queryInterface
      .dropTable('people');
  }
};

my tasks table migration:

"use strict";

module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface
      .createTable('tasks', {
        id: {
          type: Sequelize.INTEGER,
          autoIncrement: true,
          primaryKey: true
        },
        person_id: {
          type: Sequelize.INTEGER
        },
        task: Sequelize.STRING,
        created_at: Sequelize.DATE,
        updated_at: Sequelize.DATE
      });
  },

  down: function(queryInterface, Sequelize) {
    return queryInterface
      .dropTable('tasks');
  }
};

my person.js model:

"use strict";

module.exports = function(sequelize, DataTypes) {
  var Person = sequelize.define("Person", {
    name: DataTypes.STRING
  }, {
    underscored: true,
    freezeTableName: true,
    tableName: 'people',
    classMethods: {
      associate: function(models) {
        Person.hasMany(models.Task)
      }
    }
  });

  return Person;
};

my task.js model:

"use strict";

module.exports = function(sequelize, DataTypes) {
  var Task = sequelize.define("Task", {
    task: DataTypes.STRING
  }, {
    freezeTableName: true,
    tableName: 'tasks',
    classMethods: {
      associate: function(models) {
        Task.belongsTo(models.Person, {
          onDelete: "CASCADE",
          foreignKey: {
            allowNull: false
          }
        });
      }
    }
  });

  return Task;
};
like image 895
Pavan Katepalli Avatar asked May 18 '16 18:05

Pavan Katepalli


People also ask

How do you create a many-to-many relationship in Sequelize?

exports = db; belongsToMany() provides simple way to define the Sequelize Many-to-Many relationship. By passing "tutorial_tag" to through above, Sequelize will automatically generate a model named tutorial_tag as the through table (junction table), with two columns: tag_id and tutorial_id.

How do you do associations in Sequelize?

Creating associations in sequelize is done by calling one of the belongsTo / hasOne / hasMany / belongsToMany functions on a model (the source), and providing another model as the first argument to the function (the target). hasOne - adds a foreign key to the target and singular association mixins to the source.

How do I set default value Sequelize?

When you create a Sequelize model, you can add the default value for your model by adding the defaultValue option to the column(s) definition. The defaultValue option will be used by Sequelize to define default value(s) for your SQL column(s) when you create a table using Sequelize.

How do you set a foreign key in Sequelize?

Sequelize association methods also accept an options object that you can use to configure the details of the association. For example, you can change the foreign key name on the table by adding the foreignKey property: User. hasOne(Invoice, { foreignKey: "invoice_creator", // UserId -> invoice_creator });


2 Answers

Instead of using lowercase name for sequelize.define you can also set the name via alias using 'as' option. Ex. Task.belongsTo(models.Person, {as: 'task'});. Alternatively, you can set the alias in the define function itself. Ex. sequelize.define("Task", attributes, { name : { singular: "task", plural: "tasks" })

like image 135
rjoniuqa Avatar answered Oct 15 '22 20:10

rjoniuqa


define model name in lowercase as

var Task = sequelize.define("task", {

like image 35
shivshankar Avatar answered Oct 15 '22 21:10

shivshankar