通过 Flask-sqlalchemy 在 postgresql 数据库中创建 table 时出现语法错误
Syntax error while creating a table in a postgresql db via Flask-sqlalchemy
我正在尝试使用 flask-sqlalchemy 在 AWS EC2 实例中的 postgresql 数据库中创建一个新的 table,如下所示:
def table_create():
conn = psycopg2.connect(
dbname='XYZ',
user='ABC',
host='ec2-xx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com',
password='mypassword')
cursor = conn.cursor()
create_table_query = '''CREATE TABLE price_ht
(ID SERIAL PRIMARY KEY NOT NULL,
country_code CHAR(2) NOT NULL,
value float(2) NOT NULL,
start timestamp with time zone NOT NULL,
end timestamp with time zone NOT NULL,
price_type VARCHAR(32) NOT NULL)'''
cursor.execute(create_table_query)
cursor.execute("CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;")
cursor.execute(
"SELECT create_hypertable('price_ht', 'id', chunk_time_interval=>24)")
conn.commit()
print("Table created")
当我 运行 代码时,出现以下错误:
Exception has occurred: SyntaxError syntax error at or near "end" LINE
6: end timestamp with time zone NOT NULL,
存储的值必须如下所示:
谁能告诉我我做错了什么?
end
是 Postgresql 中的保留字。通常 "bad practice" 在命名事物时使用它们,在您的情况下会产生语法错误。
如果您真的想要使用它 - 您必须将它包含在 "
.
中
CREATE TABLE price_ht
(ID SERIAL PRIMARY KEY NOT NULL,
country_code CHAR(2) NOT NULL,
value float(2) NOT NULL,
start timestamp with time zone NOT NULL,
"end" timestamp with time zone NOT NULL,
price_type VARCHAR(32) NOT NULL)
我正在尝试使用 flask-sqlalchemy 在 AWS EC2 实例中的 postgresql 数据库中创建一个新的 table,如下所示:
def table_create():
conn = psycopg2.connect(
dbname='XYZ',
user='ABC',
host='ec2-xx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com',
password='mypassword')
cursor = conn.cursor()
create_table_query = '''CREATE TABLE price_ht
(ID SERIAL PRIMARY KEY NOT NULL,
country_code CHAR(2) NOT NULL,
value float(2) NOT NULL,
start timestamp with time zone NOT NULL,
end timestamp with time zone NOT NULL,
price_type VARCHAR(32) NOT NULL)'''
cursor.execute(create_table_query)
cursor.execute("CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;")
cursor.execute(
"SELECT create_hypertable('price_ht', 'id', chunk_time_interval=>24)")
conn.commit()
print("Table created")
当我 运行 代码时,出现以下错误:
Exception has occurred: SyntaxError syntax error at or near "end" LINE 6: end timestamp with time zone NOT NULL,
存储的值必须如下所示:
谁能告诉我我做错了什么?
end
是 Postgresql 中的保留字。通常 "bad practice" 在命名事物时使用它们,在您的情况下会产生语法错误。
如果您真的想要使用它 - 您必须将它包含在 "
.
CREATE TABLE price_ht
(ID SERIAL PRIMARY KEY NOT NULL,
country_code CHAR(2) NOT NULL,
value float(2) NOT NULL,
start timestamp with time zone NOT NULL,
"end" timestamp with time zone NOT NULL,
price_type VARCHAR(32) NOT NULL)