MSSQL DateTime Error in SQLAlchemy 0.3.11

Issue #842 resolved
Former user created an issue

SQLAlchemy uses a bugged check to verify that the value passed to the MSDateTime_pyodbc function. This uses a check for a second element to value that may not exist (especially if a string is used). The example code has been tested for a string and datetime object.[BR] [BR] File: \sqlalchemy\databases\mssql.py Line: 143

Example Lines: 1) String [BR] user = User()[BR] ...[BR] user.registered='2007-10-30 8:00:00' [BR] ...[BR] user.flush()[BR]

Returns:[BR] AttributeError: 'str' object has no attribute 'year' [BR]

2) DateTime [BR] ...[BR] user.registered=datetime.datetime(2007, 10, 30, 8, 0, 0)[BR] ...[BR] user.flush()[BR]

Returns: <No errors>

Faulty Line:[BR] if value and not hasattr(value, 'second'):[BR] Replacement Line:[BR] if isinstance(value, datetime.date) or isinstance(value, datetime.datetime):[BR]

''NOTE'': Attached file only shows pyodbc fixed as this may also need to be tested with the other modules.

Comments (8)

  1. Former user Account Deleted

    An additional note that environment this was tested on was SQL Server 2005 (version 9.00.1399.00) running on Windows Server 2003 R2 and Python 2.5.1.

  2. Former user Account Deleted

    Also, the time portion for the DateTime (example 2) will still be ignored, just as in the original code, perhaps the OR should be split into an if and elif if the datetime should be preserved? The string works as normal.

  3. Former user Account Deleted

    (original author: ram) Passing date strings to a DATE or DATETIME field is unsupported, instead pass a datetime.date or datetime.datetime.

    Also a clarification: the time is not "ignored" for DATETIME fields. The check for hasattr('second') is designed to see if a datetime.date() instance has been passed instead of a datetime.datetime() instance, and to upgrade the date() into a datetime() with empty time values, (or for DATE fields, to downgrade a datetime() instance to a date() instance).

  4. Former user Account Deleted
    • removed status
    • changed status to open

    ram,

    While I can appreciate the issues you bring up, I will reopen this ticket based on the grounds that the SQLAlchemy MSSQL driver behaviour is ''inconsistent'' with the behaviour of the PostgreSQL driver in this respect -- either way, one of the drivers is incorrect (either both or neither should accept a date string). I can send a date string to PostgreSQL in my Pylons 0.9.6 websetup.py script and it will work while it completely dies on MSSQL.

    The actual flaw may perhaps be occurring elsewhere in the driver; however, this is the section that killed my setup script and I felt it was important to note the functionality difference between the MSSQL and PostgreSQL drivers. I hope I have presented this case better now.

  5. paj

    I have to agree with Rick here, passing in date/datetime objects is the correct way. Strings may happen to work with Postgres, but that shouldn't be relied upon.

  6. Log in to comment