postgresql 未处理的拒绝 SequelizeDatabaseError:关系“用户”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51267470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:43:41  来源:igfitidea点击:

Unhandled rejection SequelizeDatabaseError: relation "users" does not exist

javascriptnode.jspostgresqlsequelize.jspgadmin

提问by Ahmed Ghrib

I am getting started with Sequelize. I am following the documentation they are providing on their website :http://docs.sequelizejs.com/manual/installation/getting-started.html

我正在开始使用 Sequelize。我正在关注他们在其网站上提供的文档:http: //docs.sequelizejs.com/manual/installation/getting-started.html

const Sequelize = require('sequelize');
const sequelize = new Sequelize('haha', 'postgres', 'postgres', {
  host: 'localhost',
  dialect: 'postgres',
  operatorsAliases: false,

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },

  // SQLite only
  storage: 'path/to/database.sqlite'
});


sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });


  const User = sequelize.define('user', {
    firstName: {
      type: Sequelize.STRING
    },
    lastName: {
      type: Sequelize.STRING
    }
  });

  // force: true will drop the table if it already exists
  User.sync({force: true}).then(() => {
    // Table created
    return User.create({
      firstName: 'John',
      lastName: 'Hancock'
    });
  });

Up until here, everything works perfectly. And the table "user" is correctly built and populated. (Although I do not understand Sequelize appends an "s" automatically to "user", any explanation.)

到这里为止,一切正常。并且“用户”表已正确构建和填充。(虽然我不明白 Sequelize 会自动将“s”附加到“user”,任何解释。)

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

However when I add the following portion of code:

但是,当我添加以下代码部分时:

User.findAll().then(users => {
  console.log(users)
})

I get this error :

我收到此错误:

Unhandled rejection SequelizeDatabaseError: relation "users" does not exist

未处理的拒绝 SequelizeDatabaseError:关系“用户”不存在

So my questions are:

所以我的问题是:

  1. Why does Sequelize add an "s" to user. (I know it makes sense but shouldn't the developer decide that)
  2. What is causing that error? I followed the documentation but it still didn't work?
  1. 为什么 Sequelize 向用户添加“s”。(我知道这是有道理的,但开发人员不应该决定)
  2. 是什么导致了这个错误?我遵循了文档,但它仍然不起作用?

回答by ccordon

When you are defining your model you can add configurations, in this case the option that you must add is freezeTableNameprevents the names from being plural.

当您定义模型时,您可以添加配置,在这种情况下,您必须添加的选项是freezeTableName防止名称为复数。

const User = sequelize.define('user', {
  firstName: {
    type: Sequelize.STRING
  },
  lastName: {
    type: Sequelize.STRING
  }
}, {
    // disable the modification of table names; By default, sequelize will automatically
    // transform all passed model names (first parameter of define) into plural.
    // if you don't want that, set the following
    freezeTableName: true,
  });

回答by JamesSchiiller

Run that code twice.

运行该代码两次。

Before running the second time, comment out the following code,

在第二次运行之前,注释掉以下代码,

// force: true will drop the table if it already exists
User.sync({force: true}).then(() => {
  // Table created
  return User.create({
    firstName: 'John',
    lastName: 'Hancock'
  });
});

回答by Sahan Dissanayaka

There is another interesting way you can avoid this. But you need to really focus on this way of implementation.

还有另一种有趣的方法可以避免这种情况。但是您需要真正关注这种实现方式。

const User = sequelize.define("user", {
    firstname: {
      type: Sequelize.STRING
    },
    lastname: {
      type: Sequelize.STRING
    }
  });

you intentionally put userhere and use usersin other places of coding(Assume sequelize will automatically transform all passed model names (first parameter of define) into plural) . This way of coding will simplify your code.

你故意放在user这里并users在其他地方使用(假设 sequelize 会自动将所有传递的模型名称(定义的第一个参数)转换为复数)。这种编码方式将简化您的代码。