Sequelize 表名更改 [英] Sequelize table name change

查看:64
本文介绍了Sequelize 表名更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已将 MySQL 数据库中的表从 users 重命名为 user.在 Express 中,我正在运行 Sequelize 并为旧的 users 表创建了一个架构.表重命名后,代码中的所有内容都从 users 更改为 userSequelize 仍在寻找专门命名为 users<的表/code> 即使在架构中更新了表名.

Have renamed a table from users to user in MySQL database. In Express I'm running Sequelize and created a schema for the old users table. With the table renamed and everything in the code changed from users to user, Sequelize is still looking for a table named specifically users even though the table name is updated in the schema.

User = db.sequelize.define('user', {
  first_name: Sequelize.STRING,
  last_name: Sequelize.STRING,
  email: Sequelize.STRING,
  password: Sequelize.STRING,
  role: Sequelize.STRING,
  created_at: Sequelize.DATE,
  updated_at: Sequelize.DATE
});

尝试运行它以用 Sequelize 生成的表覆盖该表,但它仍然创建了一个名为 users 的表

Tried running this to overwrite the table with one produced by Sequelize and it still created a table named users

User.sync({force: true}).then(() => {
  // Table created
  return User.create({
    firstName: 'test',
    lastName: 'test',
    email: 'test@example.com',
    password: 'password'
  });
});

有没有人遇到过同样的情况?我还缺少其他一些配置吗?这就是我在创建第一个表时所做的一切.

Has anyone else experienced the same? Is there some other configuration I am missing? This is all I did in the first place when creating the first table.

推荐答案

Sequelize 默认对表名进行复数化,设置 freezeTableName: true 来改变这种行为.

Sequelize pluralizes table name by default, Set freezeTableName: true to change this behavior like this.

User = db.sequelize.define('user', {
    first_name: Sequelize.STRING,
    last_name: Sequelize.STRING,
    email: Sequelize.STRING,
    password: Sequelize.STRING,
    role: Sequelize.STRING,
    created_at: Sequelize.DATE,
    updated_at: Sequelize.DATE
}, {
    freezeTableName: true
});

这篇关于Sequelize 表名更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆