SAP / sqlalchemy-hana

SQLAlchemy Dialect for SAP HANA
Apache License 2.0
128 stars 56 forks source link

Default timestamp value missing - func.now() not supported #288

Closed MDJoerg closed 3 months ago

MDJoerg commented 3 months ago

Is your feature request related to a problem? Please describe. I use the sqlalchemy lib to support anyDB in my solution. So far sqlite and postgres support all used features. Today I tested my code against a SAP HANA Cloud instance (created yesterday) and got the error "(hdbcli.dbapi.ProgrammingError) (257, 'sql syntax error: incorrect syntax near "now": line 13 col 30 (at pos 316)')".

The create table statement for the column was: CREATE TABLE some_table ( uuid CHAR(32) NOT NULL, ... created_at DATETIME DEFAULT now(), ... PRIMARY KEY (uuid) )

The syntax "now()" as default value is not supported?

Describe the solution you'd like From the python perspective the column is defined as:

from sqlalchemy import Column, ... from sqlalchemy.sql import func

Column(DateTime(timezone=True), server_default=func.now())

I think func.now() can be implemented for the SAP HANA dialect and set the accepted statement?

Describe alternatives you've considered At the moment I will remove the default values from the model and set the now() value by my code. This is not the best solution...

Additional context Not at the moment. Code examples should be enough? If not, call me.

Thanks for supporting us! Best regards Joerg

kasium commented 3 months ago

Interesting case. I see that sqlalchemy-hana could add a simple compile hook to render now as CURRENT_TIMESTAMP. Let me open a PR for this.

For reference, here is a complete and simple reproducrible scenarion. Please include such code the next time, because it allows me to easily work on issues:

from sqlalchemy import Column, DateTime, Integer, create_engine
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.sql import func

class Base(DeclarativeBase):
    pass

class TmpTable(Base):
    __tablename__ = "tmp_table"
    id = Column(Integer, primary_key=True)
    foo = Column(DateTime, server_default=func.now())

engine = create_engine("<some db>")
Base.metadata.create_all(engine)
kasium commented 3 months ago

Hi, 2.4.0 was just released