Skip to content

MSSQL DateTime Error in SQLAlchemy 0.3.11 #842

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by Anonymous

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]

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

Returns:

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.


Attachments: mssql.py | mssql_faulty.py

Activity

sqlalchemy-bot

sqlalchemy-bot commented on Oct 30, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

original mssql.py that contains the faulty code.

sqlalchemy-bot

sqlalchemy-bot commented on Oct 30, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by Anonymous:

  • attached file mssql_faulty.py
sqlalchemy-bot

sqlalchemy-bot commented on Oct 30, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

mssql.py file that contains the update to MSDate_pyodbc on line 143-144 with a comment.

sqlalchemy-bot

sqlalchemy-bot commented on Oct 30, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by Anonymous:

  • attached file mssql.py
sqlalchemy-bot

sqlalchemy-bot commented on Oct 30, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

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.

sqlalchemy-bot

sqlalchemy-bot commented on Oct 30, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

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.

sqlalchemy-bot

sqlalchemy-bot commented on Nov 1, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

(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).

sqlalchemy-bot

sqlalchemy-bot commented on Nov 1, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by Anonymous:

  • added labels: wontfix
  • changed status to closed
sqlalchemy-bot

sqlalchemy-bot commented on Nov 2, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

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.

sqlalchemy-bot

sqlalchemy-bot commented on Nov 2, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by Anonymous:

  • changed status to reopened
sqlalchemy-bot

sqlalchemy-bot commented on Nov 5, 2007

@sqlalchemy-bot
CollaboratorAuthor

paj (@paj) wrote:

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.

sqlalchemy-bot

sqlalchemy-bot commented on Nov 5, 2007

@sqlalchemy-bot
CollaboratorAuthor

Changes by paj (@paj):

  • added labels: wontfix
  • changed status to closed
sqlalchemy-bot

sqlalchemy-bot commented on Dec 21, 2007

@sqlalchemy-bot
CollaboratorAuthor

Anonymous wrote:

(original author: ram) I've loosened the check in 116e19f, see if that works

2 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    SQL ServerMicrosoft SQL Server, e.g. mssqlbugSomething isn't workinghigh priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          MSSQL DateTime Error in SQLAlchemy 0.3.11 · Issue #842 · sqlalchemy/sqlalchemy