propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 396 forks source link

scheme.xml wrongly generate default value for CURRENT_TIMESTAMP(6) #1905

Open Brekeke opened 2 years ago

Brekeke commented 2 years ago

If I set in SQL default value for timestamp data type column as current_timestamp(6) it's generated like:

<column name="created" phpName="Created" type="TIMESTAMP" size="6" defaultValue="current_timestamp(6)"/>

But when is called module:build from that scheme.xml it return this error:

conhost_2022-08-26_12-47-21

When I change defaultValue to defaultExpr in scheme.xml manually it works. Working part of code looks like:

<column name="created" phpName="Created" type="TIMESTAMP" size="6" defaultExpr="current_timestamp(6)"/>

So issue is in generator of scheme.xml which wrongly use defaultValue for current_timestamp(6) and another similar values instead of defaultExpr like it do for normal current_timestamp without length definition.

If is possible fix it, should be nice, thanks :-)

mringler commented 2 years ago

Yes, that is a bit confusing. But I think it is the expected behavior according to the docs:

  • defaultValue The default value that the object will have for this column in the PHP instance after creating a “new Object”. This value is always interpreted as a string.
  • defaultExpr The default value for this column as expressed in SQL. This value is used solely for the “sql” target which builds your database from the schema.xml file. The defaultExpr is the SQL expression used as the “default” for the column.

I think defaultValue has to be something that PHP understands, like new DateTime(), and if you want to use a default value on database level, you need to use defaultExpr, as you do now, and it seems to work.

So it sounds like it is working as expected, but maybe I just misunderstand your issue, then feel free to clarify.

adjenks commented 1 year ago

now() as a default value in postgres in a date column also fails when trying to run model:build. Changing it to defaultExpr also fixes it for me.

adjenks commented 1 year ago

Actually, it looks like most of my date columns set to now() were generated as defaultExpr but just one of them wasn't. This particular one has brackets around it, so perhaps it was interpreted as being a value for some reason. It's defined like so: date_valid date NOT NULL DEFAULT (now())::date