I have a users table, I see it in pgadmin4, but for some reason when I use psql and try to run list users, I get the following error:
Relation “users” does not exist.
asked Jun 20, 2018 at 0:07
This will happen if the psql user does not have schema level privileges. This error message can be misleading.
To solve this issue, try connecting using psql with an admin user and run:
1.
GRANT USAGE ON SCHEMA public TO <non-admin-user>;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO <non-admin-user>;
Cody Gray♦
236k50 gold badges486 silver badges567 bronze badges
answered Jun 20, 2018 at 0:12
You need to put table name in quotes.
And, it is case sensitive:
SELECT * FROM "Users";
Cody Gray♦
236k50 gold badges486 silver badges567 bronze badges
answered Nov 21, 2020 at 3:52
1
In case of automated test, setting a delay after migrations do the job:
setTimeout(() => {
// queries
}, 1000);
Maybe it is the delay for the database to be done.
The automated test is multithread in my case.
answered Aug 14, 2022 at 12:48
niomuniomu
174 bronze badges
I’ve add new Schema (and remove there my user table) in database so my request was
SELECT "role" FROM "user" ...
But now should be with schema name
SELECT "role" FROM "schemaName"."user" ...
answered Oct 25, 2022 at 5:08
Posted on Dec 24, 2021
When you’re running Sequelize code to fetch or manipulate data from a PostgreSQL database, you might encounter an error saying relation <table name> does not exist
.
For example, suppose you have a database named User
in your PostgreSQL database as shown below:
cakeDB=# dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------------
public | User | table | nsebhastian
(1 row)
In the above output from psql
, the cakeDB
database has one table named User
that you need to retrieve the data using Sequelize findAll()
method.
Next, you create a new connection to the database using Sequelize and create a model for the User
table:
const { Sequelize } = require("sequelize");
const sequelize = new Sequelize("cakeDB", "nsebhastian", "", {
host: "localhost",
dialect: "postgres",
});
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
});
After that, you write the code to query the User
table as follows:
const users = await User.findAll();
console.log(users);
Although the code above is valid, Node will throw an error as follows:
Error
at Query.run
...
name: 'SequelizeDatabaseError',
parent: error: relation "Users" does not exist
In PostgreSQL, a relation does not exist
error happens when you reference a table name that can’t be found in the database you currently connect to.
In the case above, the error happens because Sequelize is trying to find Users
table with an s
, while the existing table is named User
without an s
.
But why does Sequelize refer to Users
while we clearly define User
in our model above? You can see it in the code below:
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
});
This is because Sequelize automatically pluralizes the model name User
as Users
to find the table name in your database (reference here)
To prevent Sequelize from pluralizing the table name for the model, you can add the freezeTableName
option and set it to true
to the model as shown below:
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
},
{
freezeTableName: true,
});
The freezeTableName
option will cause Sequelize to infer the table name as equal to the model name without any modification.
Alternatively, you can also add the tableName
option to tell Sequelize directly the table name for the model:
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
},
{
tableName: "User",
});
Once you add one of the two options above, this error should be resolved.
Please note that the model and table names in Sequelize and PostgreSQL are also case-sensitive, so if you’re table name is User
, you will trigger the error when you refer to it as user
from Sequelize:
const User = sequelize.define("User", {
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
},
{
tableName: "user", // relation "user" does not exist
});
The relation does not exist
error in Sequelize always happens when you refer to a PostgreSQL database table that doesn’t exist.
When you encounter this error, the first thing to check is to make sure that the Sequelize code points to the right table name.
This error can also occur in your migration code because you might have migration files that create a relationship between two tables.
Always make sure that you’re referencing the right table, and that you’re using the right letter casing.
What are you doing?
edit2: Remember folks, when you change your env variables, you have to restart your server/pm2 instance =) This fixed it, although I would expect a more helpful error message when host, port etc. are undefined.
Hey guys,
I am switching my node/express app from mysql
to postgresql
. Everything was pretty seamless except I had to swap some data types. When I try to run the following command I get an error.
edit: Looks like something else is up. Sequelize throws the same error for all other queries, including relation "users" does not exist
. I know this was marked as support, but mysql was working perfectly before changing to postgres, so I imagine it should also work now.
const [ serviceUser, created ] = await ServiceUserAccountModel.findOrCreate({ where: { service_user_id: '123456' }, });
relation "serviceUserAccounts" does not exist
. or with users relation "users" does not exist
const userModel = Sequelize.define('user', { // has many ServiceUserAccounts id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, email: { type: DataTypes.STRING, allowNull: false }, age: { type: DataTypes.SMALLINT, allowNull: false }, gender: { type: DataTypes.STRING, allowNull: false }, first_name: { type: DataTypes.STRING, allowNull: true }, last_name: { type: DataTypes.STRING, allowNull: true } }); const serviceUserAccountsModel = Sequelize.define('serviceUserAccount', { // belongs to User id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, display_name: { type: DataTypes.STRING, allowNull: true }, email_address: { type: DataTypes.STRING, allowNull: true }, service_id: { type: DataTypes.SMALLINT, allowNull: true, }, service_user_id: { type: DataTypes.STRING, allowNull: true, }, refresh_token: { type: DataTypes.STRING, allowNull: true }, access_token: { type: DataTypes.STRING, allowNull: true }, token_type: { type: DataTypes.STRING, allowNull: true }, expiration_date: { type: DataTypes.INTEGER, allowNull: true }, storage_limit: { type: DataTypes.INTEGER, allowNull: true }, storage_usage: { type: DataTypes.INTEGER, allowNull: true }, trashed_storage_usage: { type: DataTypes.INTEGER, allowNull: true }, }); // Relations module.exports = function( database ){ const User = database.models.user.user; const ServiceUserAccounts = database.models.user.serviceUserAccounts; User.hasMany(ServiceUserAccounts); ServiceUserAccounts.belongsTo(User); };
What do you expect to happen?
As it was working perfectly before with mysql dialect, I expect it to also work with Postgresql.
What is actually happening?
relation "serviceUserAccounts" does not exist
. I’m able to run the query just fine in pgAdmin, so it must be something with sequelize. What am I missing?
Here’s the gist with the stacktrace
https://gist.github.com/Mk-Etlinger/569093387a0cb97699acfcba3994f59d
Any ideas? I checked my permissions and it came back public, $user
.
also looked here but no luck:
https://stackoverflow.com/questions/28844617/sequelize-with-postgres-database-not-working-after-migration-from-mysql
https://stackoverflow.com/questions/946804/find-out-if-user-got-permission-to-select-update-a-table-function-in-pos
Dialect: postgres
Dialect version: XXX
Database version: 9.6.2
Sequelize version: ^4.38.1
Tested with latest release: Yes, 4.39.0
Note : Your issue may be ignored OR closed by maintainers if it’s not tested against latest version OR does not follow issue template.