Error Relation does not exist

橙三吉。 提交于 2020-01-14 07:37:11

问题


I am getting a [error: relation "causes" does not exist] error from my node app. The relation does exist, I'm not sure what the problem is.

I created the table with

CREATE TABLE causes (

cause_id bigint NOT NULL default nextval('causes_cause_id_seq'::regclass),
cause_name varchar(40) NOT NULL,
goal integer,
sponsor varchar(30),
organization varchar(30),
submitter varchar(30),
address varchar(34),
balance numeric

);

This is the query that's giving the error:

client = pg.connect(connectionString, function(err, client, done){
    if(err) console.log(err);

    client.query('INSERT INTO causes (cause_name, goal, organization, sponsor, submitter) VALUES ($1,$2,$3,$4,$5) RETURNING *', r, function(err, result){
    if(err) console.log(err);
    });
});

回答1:


This is probably a case folding issue. See this answer and the PostgreSQL documentation on SQL syntax.

After edit: Looks like it isn't a case folding issue. Check search_path (SHOW search_path or SELECT current_setting('search_path')) and compare it to the schema the table is in (\dt+ tablename) to make sure the table is on the client's path.

Also make sure you're connecting to the same database.




回答2:


Directly before your client.query('INSERT...') call, run the following to ensure that your relation is accessible on the current connection:

client.query('SELECT * FROM pg_catalog.pg_tables', function(err, result) {
  console.log(result);
});

If you don't see your causes relation among the results, then either the relation doesn't exist, or it was created under a different user.




回答3:


I had a similar error when trying to create a table with user input for table name and then insert into database.

The solution I found was to create the create table and insert into as two separate functions, call the functions back to back, but the second one with a delay.

Guiding code is below:

function createTable(){
// your create table query here
}

function pushData(){
// your insert into table query here
}

createTable()
setTimeout(postTable, 3000); // for 3 seconds, can make is less, I used 3 secs to be safe



回答4:


I had the same problem. I was querying newly created table Example. This was my code:

const { Pool, Client } = require('pg');
const dbClient = new Client({
  user: 'postgres',
  host: 'localhost',
  database: 'postgres',
  password: 'test',
  port: 5432,
});
dbClient.connect();
dbClient.query('SELECT * from Example', (err, res) => {
    console.log(err, res);
    dbClient.end();
});

I double checked connection parameters and any possible typos. It turned out that in pg you need to wrap the table name in quotes:

dbClient.query('SELECT * from "Example"', (err, res) => {
    console.log(err, res);
    dbClient.end();
});


来源:https://stackoverflow.com/questions/19941922/error-relation-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!