postgresql Sqlalchemy + psycopg:无法在DEFAULT表达式中使用列引用

ygya80vv  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(183)

我试图在Postgres db上使用Sqlalchemy创建下面的表:

from sqlalchemy import Column, DateTime, Float, Integer, String, func
from sqlalchemy.orm import declarative_base

Base = declarative_base()

class Test(Base):
    __tablename__ = "tests"

    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True)
    create_date = Column(DateTime, server_default=func.sysdate())

运行时:

Test.__table__.create(bind=engine, checkfirst=True)

我得到以下错误:

Traceback (most recent call last):
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 747, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.FeatureNotSupported: cannot use column reference in DEFAULT expression
LINE 5:  create_date TIMESTAMP WITHOUT TIME ZONE DEFAULT sysdate,
                                                         ^

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/matteo/Documents/DataKind/MyProject/application/backend/src/app/db/import_data.py", line 22, in <module>
    Test.__table__.create(bind=engine, checkfirst=True)
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 1149, in create
    bind._run_ddl_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3220, in _run_ddl_visitor
    conn._run_ddl_visitor(visitorcallable, element, **kwargs)
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2427, in _run_ddl_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py", line 670, in traverse_single
    return meth(obj, **kw)
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py", line 958, in visit_table
    CreateTable(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py", line 315, in _invoke_with
    return bind.execute(self)
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1414, in execute
    return meth(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py", line 181, in _execute_on_connection
    return connection._execute_ddl(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1526, in _execute_ddl
    ret = self._execute_context(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1842, in _execute_context
    return self._exec_single_context(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1983, in _exec_single_context
    self._handle_dbapi_exception(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2325, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
    self.dialect.do_execute(
  File "/Users/matteo/Documents/DataKind/MyProject/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 747, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.NotSupportedError: (psycopg2.errors.FeatureNotSupported) cannot use column reference in DEFAULT expression
LINE 5:  create_date TIMESTAMP WITHOUT TIME ZONE DEFAULT sysdate,
                                                         ^

[SQL:
CREATE TABLE tests (
    id SERIAL NOT NULL,
    name VARCHAR,
    create_date TIMESTAMP WITHOUT TIME ZONE DEFAULT sysdate,
    PRIMARY KEY (id),
    UNIQUE (name)
)

]
(Background on this error at: https://sqlalche.me/e/20/tw8g)
yyyllmsg

yyyllmsg1#

更换
server_default = func. sysdate()

server_default = text('CURRENT_TIMESTAMP')

server_default = text('LOCALTIMESTAMP ')
func. sysdate()-不是PostgreSQL的函数。阅读更多here.

相关问题