xnuinside / omymodels

O!My Models (omymodels) is a library to generate Pydantic, Dataclasses, GinoORM Models, SqlAlchemy ORM, SqlAlchemy Core Table, Models from SQL DDL. And convert one models to another.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`NOW()` not recognized as `now()`

dargueta opened this issue · comments

Describe the bug

NOW() is not recognized as equivalent to now() when used as a default value, even though SQL is case-insensitive.

To Reproduce

Given the following SQL:

ddl = """
CREATE TABLE asdf (
  t TIMESTAMP DEFAULT NOW()
);
"""
print(omymodels.create_models(ddl=ddl, models_type="dataclass")["code"])

The output is

import datetime
from dataclasses import dataclass


@dataclass
class Asdf:

    t: datetime.datetime = 'NOW()'  # Default value is wrong

Note that, while the datatype of the column is correct, the default value is a string instead of a call to datetime.datetime.now(). If we make NOW()` lowercase, we get the expected output.

import datetime
from dataclasses import dataclass


@dataclass
class Asdf:

    t: datetime.datetime = datetime.datetime.now()

n.b. the expected output has a bug in it which I'll file a separate ticket for

Expected behavior

SQL is case-insensitive, therefore now() and NOW() should behave identically and produce the same output.

Additional context

Python: 3.8.2
Version: 0.8.1

@dargueta thanks for the report, I will fix it as soon as possible

Thanks!