简体   繁体   English

SQLAlchemy在MySQL上用于“Text”的列类型是什么?

[英]What column type does SQLAlchemy use for “Text” on MySQL?

My overall use-case is trying to determine whether I can write a somewhat database agnostic (at least supporting Postgres and MySQL) store for some large data as raw text (think ~500MB as a rough theoretical upper bound). 我的整个用例是试图确定我是否可以编写一些数据库不可知(至少支持Postgres和MySQL)存储一些大数据作为原始文本(认为~500MB作为粗略的理论上限)。 Based on this answer about MySQL string/text types, it looks like only the LONGTEXT column type can meet my requirements. 基于这个关于MySQL字符串/文本类型的答案 ,看起来只有LONGTEXT列类型才能满足我的要求。 I'm using SQLAlchemy, which claims for its Text column type that it is for variable length strings, but also that it generally maps to the database's CLOB or TEXT types. 我正在使用SQLAlchemy,它声称它的Text列类型是可变长度字符串,但它通常也映射到数据库的CLOB或TEXT类型。 MySQL doesn't have a CLOB type (though it does have a BLOB), and it's TEXT type would be insufficient for my needs. MySQL没有CLOB类型(虽然它有一个BLOB),它的TEXT类型不足以满足我的需求。

So, What column type does SQLAlchemy use for "Text" on MySQL? 那么, SQLAlchemy在MySQL上用于“Text”的列类型是什么?

Looks like SQLAlchemy supports LONGTEXT: 看起来像SQLAlchemy支持LONGTEXT:

$ python
Python 2.7.13 (default, Sep 29 2017, 15:31:18) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlalchemy.dialects.mysql import LONGTEXT
>>> 

See how to use vendor-specific types here: http://docs.sqlalchemy.org/en/latest/core/type_basics.html#vendor-specific-types 请在此处查看如何使用特定于供应商的类型: http//docs.sqlalchemy.org/en/latest/core/type_basics.html#vendor-specific-types

For what it's worth, trying to develop a totally brand-neutral database layer is difficult, and rarely worth the effort. 对于它的价值,尝试开发一个完全品牌中立的数据库层很困难,而且很少值得努力。 I worked on the Zend Framework 1.0 some years ago, and I tried to create a generic unit testing suite for all the SQL databases supported by that framework. 几年前我在Zend Framework 1.0上工作过,我试图为该框架支持的所有SQL数据库创建一个通用的单元测试套件。 I found that very few data types are supported in the same way across all implementations of SQL, despite them all claiming to support the ANSI/ISO SQL standard. 我发现SQL的所有实现都以相同的方式支持很少的数据类型,尽管它们都声称支持ANSI / ISO SQL标准。

Ultimately, you have to develop your own class hierarchy for your data layer, and implement the code slightly differently for each database-specific adapter. 最终,您必须为数据层开发自己的类层次结构,并为每个特定于数据库的适配器实现略有不同的代码。


Update: I think the news is better than we think. 更新:我认为这个消息比我们想象的要好。 I tried this test: 我试过这个测试:

t2 = Table('t2', metadata,
      Column('id', Integer, primary_key=True),
      Column('t1', String(64000)),
      Column('t2', String(16000000)),
      Column('t3', String(4294000000)),
      Column('t4', Text)
     )

metadata.create_all(engine)

Then I checked to see what it ended up creating in the MySQL database: 然后我检查了它在MySQL数据库中最终创建的内容:

mysql> show create table t2;

CREATE TABLE `t2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `t1` mediumtext,
  `t2` longtext,
  `t3` longtext,
  `t4` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

So it does map SQLAlchemy's generic String data type to a more-or-less appropriate MySQL data type. 因此,它确实将SQLAlchemy的通用String数据类型映射到一个或多或少适当的MySQL数据类型。

It's not surprising to me that it used larger data types than we might expect. 我使用比我们预期的更大的数据类型并不奇怪。 The MEDIUMTEXT supports 16MB in bytes , not in characters . MEDIUMTEXT支持16MB 字节 ,而不是字符 Because my default character set is the multi-byte utfmb4, the max length of MEDIUMTEXT is actually much fewer than 2^24 characters. 因为我的默认字符集是多字节utfmb4,的最大长度MEDIUMTEXT实际上非常少于2 ^ 24个字符。 So it had to upgrade it to LONGTEXT . 所以它必须升级到LONGTEXT Of course, 2^32 characters won't fit in LONGTEXT either, but it appears SQLAlchemy assumes you mean to create a column anyway. 当然,2 ^ 32个字符也不适合LONGTEXT ,但似乎SQLAlchemy认为你无论如何都要创建一个列。

I still think that it's hard to do totally implementation-neutral code. 我仍然认为很难完全实现中立的代码。 For example, what if you want to use some MySQL features like table options for the storage engine, or specific data types with no generic equivalent (for example, ENUM )? 例如,如果要使用某些MySQL功能(如存储引擎的表选项)或没有通用等效项的特定数据类型(例如, ENUM ),该怎么办?

In SQLAlchemy 1.2.7, the Text data type maps to MySQL Type "text", or "longtext" depending on the length you enter. 在SQLAlchemy 1.2.7中,Text数据类型映射到MySQL类型“text”或“longtext”,具体取决于您输入的长度。

body_html = Column(Text())
body_plain = Column(Text(4294000000))

Creates the following in MySQL: 在MySQL中创建以下内容:

| Field           | Type         | 
+-----------------+--------------+
| body_html       | text         |
| body_plain      | longtext     |

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

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