LinuxDoku / migratordotnet

Automatically exported from code.google.com/p/migratordotnet
0 stars 0 forks source link

Assigning a DateTime as default value to a DateTime-field renders invalid SQL (Postgres) #99

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. new Column( "visible_from", DbType.DateTime, ColumnProperty.NotNull,
DateTime.MinValue ),
2. Try to run the migration
3. Yields a syntax error

What is the expected output? What do you see instead?
CREATE TABLE common.visibility (visibility_id  serial  NOT NULL PRIMARY
KEY, external_reference varchar(255), market_id int4 , visible_from
timestamp NOT NULL DEFAULT 0001-01-01 00:00:00, visible_through timestamp
NOT NULL DEFAULT 9999-12-31 23:59:59)
Warning! : ERROR: 42601: syntax error at or near "00"

What version of the product are you using? On what operating system? With
what .NET implementation/version?
Vista, .NET 3.5

What database and version are you seeing this issue on?
Postgres 8.3

Please provide any additional information below.
This is caused because it doesn't qoute the default value correctly. It
should issue:

CREATE TABLE common.visibility (visibility_id  serial  NOT NULL PRIMARY
KEY, external_reference varchar(255), market_id int4 , visible_from
timestamp NOT NULL DEFAULT '0001-01-01 00:00:00', visible_through timestamp
NOT NULL DEFAULT '9999-12-31 23:59:59')

Even more preferrable:
'0001-01-01 00:00:00'::timestamp without time zone

A temporary workaround is to quote it yourself:
new Column( "visible_from", DbType.DateTime, ColumnProperty.NotNull,
String.Format("'{0}'", DateTime.MinValue) )

Original issue reported on code.google.com by jimmy.shimizu on 12 Mar 2009 at 11:07

GoogleCodeExporter commented 8 years ago
Defaults are currently awkard so yes, you need to quote them yourself. That 
might not
be the workaround, it might be the solution.

Defaults have to support '0' as well as (getdate()), so we can't automatically 
just
quote the string.

Original comment by geoffl...@gmail.com on 20 Mar 2009 at 1:30

GoogleCodeExporter commented 8 years ago
I just fixed the equivalent issue for Oracle (in our local copy), but went 
about it 
like this:

public override string Default(object defaultValue)
{
if (defaultValue is DateTime)
{
    DateTime dt = (DateTime) defaultValue;
    string dtString= dt.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss");
    defaultValue = 
        string.Format("to_date('{0}', 'yyyy-mm-dd hh24:mi:ss')", dtString);
} 
else if (defaultValue is string)
{
    defaultValue = string.Format("'{0}'", defaultValue);
}

return base.Default(defaultValue);
}

I would say that the dialect should do the quoting, so that dialect 
specificness 
doesn't leak out into the Migrations. Then handle functions such as getdate() 
as a 
new class specific e.g class Fn {...} that Dialect.Default can test for as 
above.

Original comment by richard....@googlemail.com on 12 Jul 2009 at 12:15