Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User model goes by the actual table name or model name? #44

Closed
zenithtekla opened this issue Aug 8, 2016 · 1 comment
Closed

User model goes by the actual table name or model name? #44

zenithtekla opened this issue Aug 8, 2016 · 1 comment

Comments

@zenithtekla
Copy link

zenithtekla commented Aug 8, 2016

Hi,
On the production floor (my workplace, proj is still pending development), I added new route directly to your app. Here how I tried with Assembly model

// model schema
"use strict";

module.exports = function(sequelize, DataTypes) {
  var Assembly = sequelize.define('orm_assembly_table', {
      customer_id: {
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false
      },
      number: DataTypes.STRING(250),
      revision: DataTypes.STRING(10),
      unique_key: {
        type: DataTypes.STRING(10),
        allowNull: false,
        unique: true
      }
    },
    {
      timestamps: false,
      freezeTableName: true

      /*, getterMethods   : {
       fullName       : function()  { return this.firstname + ' ' + this.lastname }
       },

       setterMethods   : {
       fullName       : function(value) {
       var names = value.split(' ');

       this.setDataValue('firstname', names.slice(0, -1).join(' '));
       this.setDataValue('lastname', names.slice(-1).join(' '));
       },
       },
       classMethods: {
       generateHash : function(password) {
       return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
       },
       },
       instanceMethods: {
       validPassword : function(password) {
       return bcrypt.compareSync(password, this.localpassword);
       }
       }

       */
    });

  return Assembly;
};
extends layout

block content
  h1= title
  p This is Sequelize greeting you :)

  h2 Assembly Listing

  ul
  each assembly in assemblys
    li
      = assembly.number
      |  
      | (
      a(href="/users/" + assembly.revision + "/destroy") Destroy
      | )
var models  = require('../models');
var express = require('express');
var router  = express.Router();

router.get('/', function(req, res) {
  models.orm_assembly_table.findAll().then(function(assemblys) {
    res.render('assembly', {
      title: 'Sequelize: Express Example',
      assemblys: assemblys
    });
  });
});

module.exports = router;

If all go like this, I get assemblys displayed correctly.

When I tried with Assembly in place of (au lieu de) orm_assembly_table, I encountered error of undefined. So, in a router.get(), what should be used as property of models? I tried console.log(models) the models object in models/index.js and saw something like

models: { orm_assembly_table: orm_assembly_table }

 importCache: { 'C:\inetpub\wwwroot\development\phuc_NodeJS_MySQL\NodeMySQL\models\assembly.server.mod
el.js': orm_assembly_table }

Advices? Thanks

P/S: I like to write it models.Assembly than the actual table name (orm_assembly_table, which is not nice) but I can't get it to work.

@sushantdhiman
Copy link
Contributor

@zenithtekla You should be able to use tableName option for model, something like this -

var Assembly = sequelize.define('Assembly ', {
      customer_id: {
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false
      },
      number: DataTypes.STRING(250),
      revision: DataTypes.STRING(10)
    }, {
      timestamps: false,
      freezeTableName: true,
      tableName: 'orm_assembly_table'
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants