denodrivers / postgres

PostgreSQL driver for Deno

Home Page:https://denodrivers.github.io/postgres

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PostgresError: column "admin" does not exist which admin is a table name

a3bd2lra7man opened this issue · comments

as Deno have trust in you and put you as the first choice to develop a database app
so I choose you but I got a really strong error
the exception says that column "admin" does not exist which I add admin as table name in my query

here the query code
await client.queryArray(INSERT INTO admin (name,account,password) VALUES (${admin.name},${admin.account},${admin.password}););

the full code is here

this is the function I make the call to add a new admin to the database

async insert(admin: Admin): Promise {
await this.database.performOperation(async(client)=>{
await client.queryArray(INSERT INTO admin (name,account,password) VALUES (${admin.name},${admin.account},${admin.password}););
})
}

and here is my database class

export class PostgresDatabase extends Database {
databaseUrl = Deno.env.get("DATABASE_URL")!;

private pool = new postgres.Pool(this.databaseUrl, 3, true);

async connect(): Promise<void> {
  // no implantation
}

async performOperation(operationCallBack:(client :postgres.PoolClient)=>Promise<void>){
    const client = await this.connectToPostgres();
    try{
        await operationCallBack(client);
    }catch(e){
        console.log(e);
        throw new BadDatabaseOperation();
    }
    finally{
        client.release();
        await this.pool.end();
    }
}

private async connectToPostgres(): Promise<postgres.PoolClient> {
    try {
        const client = await this.pool.connect();
        return client;
    } catch (_) {
        throw new BadDatabaseConnection();
    }
}

}

I hope I got the help I need

this is the exception

PostgresError: column "admin" does not exist
at Connection.#simpleQuery (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:626:19)
at async Connection.query (https://deno.land/x/postgres@v0.14.2/connection/connection.ts:875:16)

If the syntax is just as you described, the error lies within the syntax you are using to execute the query

Let's say admin is the following object

const admin = {
  name: "admin",
  account: "some_account",
  password: "some_password",
};
await client.queryArray(`INSERT INTO admin (name, account, password) VALUES (${admin.name},${admin.account},${admin.password});`);

The code above will execute the query as the following:

INSERT INTO admin (name, account, password) VALUES (admin, some_account, some_password);

Problem is, the code above is invalid since strings need to be wrapped in single quotes in order to be interpreted as such. It can be fixed by using any of the following syntaxis. They both will replace the arguments at runtime and add the required quotes for it to work correctly

await client.queryArray(
  `INSERT INTO admin (name, account, password) VALUES ($1, $2, $3);`,
  admin.name,
  admin.account,
  admin.password,
);
// or
// Notice how there aren't parentheses wrapping the function call
await client.queryArray`INSERT INTO admin (name, account, password) VALUES (${admin.name}, ${admin.account}, ${admin.password})`;

I recommend you read the driver manual before progressing any further into development https://deno-postgres.com/

@Soremwar
Thanks it was my mistake