37

Is it possible to have column names be underscored (postgres) but have the JavaScript getters be camelCase per language standards?

4

4 回答 4

45

对于以后发现这一点的任何人,现在都可以明确定义数据库字段的名称:

var User = sequelize.define('user', {
  isAdmin: {
    type: DataTypes.BOOLEAN,
    field: 'is_admin'
  }
});
于 2014-09-24T07:36:26.773 回答
13

2019+

在 Sequelize v5 中,您现在可以使用该underscored: true属性


const User = sequelize.define('User', {
    username: DataTypes.STRING,
    password: DataTypes.STRING
  }, {underscored: true});

https://sequelize.org/master/class/lib/model.js~Model.html#static-method-init

于 2019-07-16T12:50:21.347 回答
2

不直接在您的列定义中,但您可以查看 getter 和 setter:

http://sequelizejs.com/documentation#models-getters---setters-defining-as-part-of-the-model-options

尽管此选项要求您手动为每列定义 getter 和 setter,但它不能自动化。此外,您的 getter 和实际的列名都将在对象上可用。

我认为 github 上的此功能存在问题,但我现在找不到


实际链接 https://sequelize.org/master/manual/models-definition.html#getters--amp--setters

于 2013-09-20T11:46:31.993 回答
1

您可以使用更新版本的 Sequelize 为模型(表)和键(字段)实现此目的。

我正在使用 4.29.2,我的模型如下所示:

const Post = sequelize.define('post', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      allowNull: false,
    },
    isActive: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      allowNull: false,
      field: 'is_active',
    },
    isDeleted: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      allowNull: false,
      field: 'is_deleted',
    },
  }, {
    indexes: [
      {
        unique: false,
        fields: ['is_active'],
      },
      {
        unique: false,
        fields: ['is_deleted'],
      },
    ],
    defaultScope: {
      where: {
        isActive: true,
        isDeleted: false,
      },
    },
  });

const PostComments = sequelize.define('postComments', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      allowNull: false,
    },
    postId: {
      type: DataTypes.UUID,
      allowNull: false,
      field: 'post_id',
    },
    comment: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
    tableName: 'post_comments',
    indexes: [
      {
        unique: false,
        fields: ['post_id'],
      },
    ],
  });


  Post.hasMany(PostComments, {
    foreignKey: 'postId',
    constraints: true,
    as: 'comments',
  });

  PostComments.belongsTo(Post, {
    foreignKey: 'postId',
    constraints: true,
    as: 'post',
  });

如您所见,我正在设置模型(表)的tableName值和键(字段)的字段值。

当我运行时:

sequelize.query('SET FOREIGN_KEY_CHECKS = 0', { raw: true })
  .then(() => {
    conn.sync({ force: true }).then(() => {
      console.log('DONE');
    });
  });

结果是:

Executing (default): SET FOREIGN_KEY_CHECKS = 0
Executing (default): DROP TABLE IF EXISTS `post_comments`;
Executing (default): DROP TABLE IF EXISTS `post`;
Executing (default): CREATE TABLE IF NOT EXISTS `post` (`id` CHAR(36) BINARY NOT NULL , `is_active` TINYINT(1) NOT NULL DEFAULT true, `is_deleted` TINYINT(1) NOT NULL DEFAULT false, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `post_comments` (`id` CHAR(36) BINARY NOT NULL , `post_id` CHAR(36) BINARY NOT NULL, `comment` VARCHAR(255) NOT NULL, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
于 2018-02-27T01:26:54.507 回答