Skip to content

When i add "underscored" attribute, creating 2 table. #10971

Closed

Description

@yusufilkeroguz

Hello;

I created a model "FooBars" and i added "--underscored".

sequelize model:generate --name "FooBars" --attributes "name:string" --underscored

Table created and name is FooBars. Everthing seems ok. now i am calling "FooBars" model in my router.js

// Require all models
const db = require('./../models/index')

db.FooBars.sync({ force:process.env.DROP_TABLES })

Output:

Executing (default): CREATE TABLE IF NOT EXISTS `foo_bars` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;

Screen Shot 2019-05-20 at 11 17 39

MySQL version: 5.6
Sequelize version: ^5.8.6
Node Version: 8.15.0
OS: 10.14.3

Activity

  1. yusufilkeroguz commented on May 20, 2019

    @yusufilkeroguz
    Author

    Excuse me;

    i am adding models/index.js and models/fooBars.js

    FooBars

    'use strict';
    module.exports = (sequelize, DataTypes) => {
      const FooBars = sequelize.define('FooBars', {
        name: DataTypes.STRING
      }, {
        underscored: true,
      });
      FooBars.associate = function(models) {
        // associations can be defined here
      };
      return FooBars;
    };

    Index

    'use strict';
    
    const fs = require('fs');
    const path = require('path');
    const Sequelize = require('sequelize');
    const basename = path.basename(__filename);
    const env = process.env.NODE_ENV || 'development';
    const config = require(__dirname + '/../config/config.js')[env];
    const db = {};
    
    let sequelize;
    if (config.use_env_variable) {
      sequelize = new Sequelize(process.env[config.use_env_variable], config);
    } else {
      sequelize = new Sequelize(config.database, config.username, config.password, config);
    }
    
    fs
      .readdirSync(__dirname)
      .filter(file => {
        return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
      })
      .forEach(file => {
        const model = sequelize['import'](path.join(__dirname, file));
        db[model.name] = model;
      });
    
    Object.keys(db).forEach(modelName => {
      if (db[modelName].associate) {
        db[modelName].associate(db);
      }
    });
    
    db.sequelize = sequelize;
    db.Sequelize = Sequelize;
    
    module.exports = db;
  2. self-assigned this
    on Jul 23, 2019
  3. papb commented on Jul 25, 2019

    @papb
    Member

    Closing in favor of #11225.

    When #11225 gets closed, if anyone thinks this particular issue is still an issue, please ping me.

  4. waulkrie commented on May 23, 2024

    @waulkrie

    For anyone who is upgrading from v4 to v6 and is having this bug use the tableName attribute in the model

      const FooBars = sequelize.define('FooBars', {
        name: DataTypes.STRING
      }, {
        underscored: true,
        tableName: 'FooBars',
      });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions