问题
在使用 SQLAlchemy 时:
1 | db.Column(db.Text) |
这两者有什么区别?
回答
根据 PostgreSQL 官方文档:
If character varying is used without length specifier, the type accepts strings of any size. The latter is a PostgreSQL extension.
In addition, PostgreSQL provides the text type, which stores strings of any length. Although the type text is not in the SQL standard, several other SQL database management systems have it as well.
简而言之:
character varying
(VARCHAR) 如果不指定长度,可以接受任意大小的字符串(PostgreSQL 扩展)text
类型可以存储任意长度的字符串,虽然不在 SQL 标准中,但许多其他 SQL 数据库管理系统也支持它
在 PostgreSQL 中,VARCHAR 和 TEXT 在性能上几乎没有区别,主要区别在于语义和标准兼容性。