简体   繁体   English

SQLAlchemy Query不返回所有大文本字段

[英]SQLAlchemy Query not returning all of large text field

First up, here's a little bit about my environment: 首先,这里有一点关于我的环境:

  • Using MSSMS as my database management tool. 使用MSSMS作为我的数据库管理工具。
  • Using Django 1.3 使用Django 1.3
  • Using SQLAlchemy 使用SQLAlchemy

I have a text field in the database with no max length. 我在数据库中有一个没有最大长度的文本字段。 There is text in it that is 890591 characters long. 其中有文本长度为890591个字符。

When I retrieve this field using SQLAlchemy it is truncated to 64512 characters. 当我使用SQLAlchemy检索此字段时,它被截断为64512个字符。 I've tried it with several other large rows too, and it's always truncated to 64512. 我也尝试过其他几个大行,它总是被截断为64512。

SELECT @@TEXTSIZE returns some absurd value like 1.6 million, so that's not the problem. SELECT @@TEXTSIZE返回一些荒谬的值,如160万,所以这不是问题。 If I do SELECT DATALENGTH(field) it returns the correct 890591. So it doesn't seem to be the database, it seems to be SQLAlchemy . 如果我执行SELECT DATALENGTH(field)它返回正确的890591.所以它似乎不是数据库,它似乎是SQLAlchemy Or perhaps it could be some Python limit. 或许它可能是一些Python限制。

Any ideas? 有任何想法吗? I seem to be at my wits end. 我似乎在我的智慧结束。

EDIT: Here's some more info that was requested: 编辑:这里有一些要求的信息:

OS: Debian 5.0.9 操作系统:Debian 5.0.9

SQLAlchemy: 0.7.3 SQLAlchemy:0.7.3

SQL: MS Sql Server 2008 SQL:MS Sql Server 2008

DB Connection: mssql+pymssql://name:password@server/dbname 数据库连接:mssql + pymssql:// name:password @ server / dbname

pymssql version: 1.0.2 pymssql版本:1.0.2

Model in question: 有问题的模型:

class RACReport(Base):
    __tablename__ = 'RACReport'
    id                      = Column(properUUID(), primary_key=True, nullable=False, default=genuuid4, server_default=text('NEWID()'))
    client_id               = Column(properUUID(), ForeignKey(Client.id), nullable=False)
    rawdata                 = Column(Text(), nullable=True)
    rawtime                 = Column(DateTime(), nullable=True, default=datetime.datetime.now())
    processeddata           = Column(Text(), nullable=True)
    processedtime           = Column(DateTime(), nullable=True)
    reportstartdate         = Column(DateTime(), nullable=False)
    reportenddata           = Column(DateTime(), nullable=False)
    numberofdocs            = Column(Integer(), nullable=True)
RACReport.__table__.schema='rac'

class properUUID(st.TypeDecorator):
    impl = mssql.MSUniqueIdentifier
    def process_result_value(self, value, dialect):
        if value:
            return str(uuid.UUID(bytes_le=value))

def genuuid4():
    return str(uuid.uuid4())

rawdata and processdata are the two fields he is having the problem with. rawdata和processdata是他遇到问题的两个领域。

Here's a test query and echo: 这是一个测试查询和回声:

rac.session.query(rac.RACReport).filter(rac.RACReport.id=='8fb76cb7-d752-45af-a20a-3b85d5e7b8a6').all()

2011-11-17 09:39:46,890 INFO sqlalchemy.engine.base.Engine SELECT [RACReport_1].id AS [rac_RACReport_id], [RACReport_1].client_id AS [rac_RACReport_client_id], [RACReport_1].rawdata AS [rac_RACReport_rawdata], [RACReport_1].rawtime AS [rac_RACReport_rawtime], [RACReport_1].processeddata AS [rac_RACReport_processeddata], [RACReport_1].processedtime AS [rac_RACReport_processedtime], [RACReport_1].reportstartdate AS [rac_RACReport_reportstartdate], [RACReport_1].reportenddate AS [rac_RACReport_reportenddate] FROM rac.[RACReport] AS [RACReport_1] WHERE [RACReport_1].id = %(id_1)s 2011-11-17 09:39:46,890 INFO sqlalchemy.engine.base.Engine {'id_1': '8fb76cb7-d752-45af-a20a-3b85d5e7b8a6'}

I know little of *nix connectivity to SQL Server, but simple googling suggests that the issue is related to FreeTDS configuration: 我对与SQL Server的* nix连接知之甚少,但简单的谷歌搜索表明该问题与FreeTDS配置有关:

  1. A related question on SO: Data ended at 64512 characters - MSSQL // PHP // OPENSuSE // APACHE2 . 关于SO的相关问题: 数据以64512个字符结束 - MSSQL // PHP // OPENSUSE // APACHE2
  2. Another Q&A extracted from here : 这里提取的另一个问答:

My text data are being truncated or are causing my client to break. 我的文本数据被截断或导致我的客户端中断。

The text data type is different from char and varchar types. 文本数据类型与char和varchar类型不同。 The maximum data length of a text column is governed by the textsize connection option. 文本列的最大数据长度由textsize连接选项控制。 Microsoft claims in their documentation to use a default textsize of 4000 characters, but in fact their implementation is inconsistent. Microsoft在其文档中声称使用4000个字符的默认文本大小,但实际上它们的实现不一致。 Sometimes text columns are returned with a size of 4 GB! 有时会返回大小为4 GB的文本列!

The best solution is to make sure you set the textsize option to a reasonable value when establishing a connection. 最佳解决方案是确保在建立连接时将textsize选项设置为合理的值。 For example: 例如:

1> set textsize 10000 
2> go 

See also the text size option in freetds.conf. 另请参见freetds.conf中的text size选项。


And just a side note: you seem to be using rather outdated version of pymssql . 而且只是旁注:您似乎使用了相当过时的pymssql版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM