0

我在 linux ubuntu 10.04 上通过 pyodbc,通过 FreeTDS odbc 驱动程序连接到 mssql 服务器。

Sqlalchemy 0.5DATETIME用于sqlalchemy.Date()字段。

现在 Sqlalchemy 0.6 使用DATE,但 sql server 2000 没有DATE类型。我怎样才能成为sqlalchemy 0.6方言DATETIME的默认设置?sqlalchemy.Date()mssql+pyodbc

我想尽可能保持清洁。

这是重现该问题的代码:

import sqlalchemy
from sqlalchemy import Table, Column, MetaData, Date, Integer, create_engine

engine = create_engine(
    'mssql+pyodbc://sa:sa@myserver/mydb?driver=FreeTDS')

m = MetaData(bind=engine)

tb = sqlalchemy.Table('test_date', m, 
    Column('id', Integer, primary_key=True),
    Column('dt', Date())
)
tb.create()

这是我得到的回溯:

Traceback (most recent call last):
  File "/tmp/teste.py", line 15, in <module>
    tb.create()
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/schema.py", line 428, in create
    bind.create(self, checkfirst=checkfirst)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1647, in create
    connection=connection, **kwargs)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1682, in _run_visitor
    **kwargs).traverse_single(element)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/sql/visitors.py", line 77, in traverse_single
    return meth(obj, **kw)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/ddl.py", line 58, in visit_table
    self.connection.execute(schema.CreateTable(table))
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1157, in execute
    params)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1210, in _execute_ddl
    return self.__execute_context(context)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1268, in __execute_context
    context.parameters[0], context=context)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1367, in _cursor_execute
    context)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/base.py", line 1360, in _cursor_execute
    context)
  File "/home/nosklo/.local/lib/python2.6/site-packages/sqlalchemy/engine/default.py", line 277, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (ProgrammingError) 
('42000', '[42000] [FreeTDS][SQL Server]Column or parameter #2: 
Cannot find data type DATE. (2715) 
(SQLExecDirectW)') 
'\nCREATE TABLE test_date (\n\tid INTEGER NOT NULL IDENTITY(1,1), 
\n\tdt DATE NULL, \n\tPRIMARY KEY (id)\n)\n\n' ()
4

3 回答 3

0

我明白了 - 我的配置错误。

事实证明,您必须配置 freetds 以使其使用 TDS 协议的 7.0 或 8.0 版本。默认情况下,它使用 4.2,在查询 MS SQL Server 版本时会产生奇怪的结果,导致 SQLAlchemy 出现令人困惑的行为,正如我在问题中所描述的那样。

我在文件上正确设置了它freetds.conf,但是没有读取该文件,因为它仅在您使用文件中定义的 DSN 时才被解析,并且我使用的是问题示例中的连接字符串。

如此处TDSVER所述设置变量解决了该问题。

于 2010-06-10T22:56:22.547 回答
0

This situation is supposed to be properly handled by sqlalchemy. See MS SQL - Date / Time Handling.

You can also this the extract of the implementation that handles this (see mssql\base.py):

def visit_date(self, type_):
    if self.dialect.server_version_info < MS_2008_VERSION:
        return self.visit_DATETIME(type_)
    else:
        return self.visit_DATE(type_)

My suggestion is to debug your code and check if this MSTypeCompiler is used and if you hit the visit_date(...) method.

于 2010-06-10T08:15:08.780 回答
0

你怎么做迁移!?

我与使用 mssqlsucks 在同一条船上。这是我的解决方案。

配置

SQLALCHEMY_DATABASE_URI = 'mssql+pyodbc://dashboarddata'
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')

我的init .py (下划线在哪里?)

我遇到了很多编码问题,我最终这样做了,而且它似乎有效。我很想看看你最后做了什么。

class HackedSQLAlchemy(SQLAlchemy):
    def apply_driver_hacks(self, app, info, options):
        print "Applying driver hacks"
        super(HackedSQLAlchemy, self).apply_driver_hacks(app, info, options)
        options["supports_unicode_binds"] = False
        # import pdb
        # pdb.set_trace()
@app.template_filter('reverse')
def reverse_filter(s):
    if s > datetime.date.today():
      return 0
    else:
       return 1

db = HackedSQLAlchemy(app)
于 2014-06-04T22:48:48.583 回答