N
Neon2y ago
rising-crimson

I need to create table with columns named "from" and "to".

Hi guys. I need to create table with columns named "from" and "to". But I get
NeonDbError: syntax error at or near "from"
NeonDbError: syntax error at or near "from"
and when I put in double quotes then I get
NeonDbError: syntax error at or near ")"
NeonDbError: syntax error at or near ")"
Please tell me how I can solve it. here is my code
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import { sql } from "drizzle-orm";
import { DATABASE_URL } from "../env";

const sql = neon(DATABASE_URL);
export const db = drizzle(sql);

export async function createTable(tableName: string): Promise<void> {
const query = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(tableName)} (
hash TEXT PRIMARY KEY,
block_hash TEXT,
block_number INTEGER,
block_time TIMESTAMP,
type TEXT,
nonce INTEGER,
gas_limit INTEGER,
gas_price TEXT,
gas_used INTEGER,
max_fee_per_gas INTEGER,
max_priority_fee_per_gas INTEGER,
priority_fee_per_gas INTEGER,
success BOOLEAN,
l1_block_number INTEGER,
l1_fee TEXT,
l1_fee_scalar INTEGER,
l1_gas_price TEXT,
l1_gas_used INTEGER,
l1_timestamp TIMESTAMP,
l1_tx_origin TEXT,
data TEXT,
from TEXT,
to TEXT,
value TEXT,
block_date TIMESTAMP,
accessList TEXT,
index INTEGER,
access_list TEXT,
max_fee_per_blob_gas INTEGER,
);
`;
await db.execute(query);

console.log(`Table ${tableName} has been created.`);
}
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import { sql } from "drizzle-orm";
import { DATABASE_URL } from "../env";

const sql = neon(DATABASE_URL);
export const db = drizzle(sql);

export async function createTable(tableName: string): Promise<void> {
const query = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(tableName)} (
hash TEXT PRIMARY KEY,
block_hash TEXT,
block_number INTEGER,
block_time TIMESTAMP,
type TEXT,
nonce INTEGER,
gas_limit INTEGER,
gas_price TEXT,
gas_used INTEGER,
max_fee_per_gas INTEGER,
max_priority_fee_per_gas INTEGER,
priority_fee_per_gas INTEGER,
success BOOLEAN,
l1_block_number INTEGER,
l1_fee TEXT,
l1_fee_scalar INTEGER,
l1_gas_price TEXT,
l1_gas_used INTEGER,
l1_timestamp TIMESTAMP,
l1_tx_origin TEXT,
data TEXT,
from TEXT,
to TEXT,
value TEXT,
block_date TIMESTAMP,
accessList TEXT,
index INTEGER,
access_list TEXT,
max_fee_per_blob_gas INTEGER,
);
`;
await db.execute(query);

console.log(`Table ${tableName} has been created.`);
}
6 Replies
quickest-silver
quickest-silver2y ago
Table names can't be a variable. I think one thing you can try is constrcut the query before passing it to the sql function
rising-crimson
rising-crimsonOP2y ago
Why table names can't be variable?
quickest-silver
quickest-silver2y ago
well you can't parameterize a table name and the tagged templates convert the variables into parameters.
rising-crimson
rising-crimsonOP2y ago
What do you mean
construct the query before passing it to the sql function
construct the query before passing it to the sql function
?I don't understand what I should do
quickest-silver
quickest-silver2y ago
So something like this:
export async function createTable(tableName: string): Promise<void> {
const statement = `CREATE TABLE IF NOT EXISTS ${sql.identifier(tableName)} (
hash TEXT PRIMARY KEY,
block_hash TEXT,
block_number INTEGER,
block_time TIMESTAMP,
type TEXT,
nonce INTEGER,
gas_limit INTEGER,
gas_price TEXT,
gas_used INTEGER,
max_fee_per_gas INTEGER,
max_priority_fee_per_gas INTEGER,
priority_fee_per_gas INTEGER,
success BOOLEAN,
l1_block_number INTEGER,
l1_fee TEXT,
l1_fee_scalar INTEGER,
l1_gas_price TEXT,
l1_gas_used INTEGER,
l1_timestamp TIMESTAMP,
l1_tx_origin TEXT,
data TEXT,
from TEXT,
to TEXT,
value TEXT,
block_date TIMESTAMP,
accessList TEXT,
index INTEGER,
access_list TEXT,
max_fee_per_blob_gas INTEGER,
);`
const query = sql(statement);
await db.execute(query);

console.log(`Table ${tableName} has been created.`);
}
export async function createTable(tableName: string): Promise<void> {
const statement = `CREATE TABLE IF NOT EXISTS ${sql.identifier(tableName)} (
hash TEXT PRIMARY KEY,
block_hash TEXT,
block_number INTEGER,
block_time TIMESTAMP,
type TEXT,
nonce INTEGER,
gas_limit INTEGER,
gas_price TEXT,
gas_used INTEGER,
max_fee_per_gas INTEGER,
max_priority_fee_per_gas INTEGER,
priority_fee_per_gas INTEGER,
success BOOLEAN,
l1_block_number INTEGER,
l1_fee TEXT,
l1_fee_scalar INTEGER,
l1_gas_price TEXT,
l1_gas_used INTEGER,
l1_timestamp TIMESTAMP,
l1_tx_origin TEXT,
data TEXT,
from TEXT,
to TEXT,
value TEXT,
block_date TIMESTAMP,
accessList TEXT,
index INTEGER,
access_list TEXT,
max_fee_per_blob_gas INTEGER,
);`
const query = sql(statement);
await db.execute(query);

console.log(`Table ${tableName} has been created.`);
}
quick question: why do you need to do this though? https://discord.com/channels/1176467419317940276/1178738361276563546/1255210082829078580 This is another possible reason why your code doesn't work
rising-crimson
rising-crimsonOP2y ago
Yes, that was the reason Thanks man for your help

Did you find this page helpful?