Repository navigation
Open
Labels
breaking changeFor issues and PRs. Changes that break compatibility and require a major version increment.
Description
Goals
- Resolve Need a VARBINARY data type #5981
- Resolve Suggestion: Please support DataTypes.UUID.BINARY #9271
- Resolve MSSQL: Implicit conversion causes table scan -- Prepending N to strings precludes index use on varchar columns #9107
- Resolve [MSSQL]DataTypes.STRING should be Varchar or NVarchar ? #6036
- Resolve How to set collation to model? #7110
DataTypes.STRING.BINARYgeneratesBINARYin some dialects (instead ofVARBINARY), andVARCHAR BINARYin mysql, which are not equivalent.
Proposition
- Rework
DataTypes.STRINGbased on the table below. AddDataTypes.BINARY. - Deprecate
DataTypes.CITEXT,DataTypes.TEXT,DataTypes.CHAR,DataTypes.BLOB
Latest design
STRING DataType
| DataType | postgres | MySQL/MariaDB | SQL Server | SQLite | DB2 | Oracle | Snowflake |
|---|---|---|---|---|---|---|---|
STRING |
VARCHAR(255) |
VARCHAR(255) |
VARCHAR(255) |
TEXT1 |
VARCHAR(255) |
VARCHAR2(255 CHAR) |
VARCHAR(255) |
STRING('max') |
VARCHAR |
❌ | VARCHAR(MAX) |
TEXT |
❌ | ❌ | VARCHAR |
STRING(256) |
VARCHAR(256) |
VARCHAR(256) |
VARCHAR(256) |
TEXT1 |
VARCHAR(256) |
VARCHAR(256 CHAR) |
VARCHAR(256) |
STRING(256).FIXED |
CHAR(256) |
CHAR(256) |
CHAR(256) |
❌ | CHARACTER(256) |
CHAR(256 CHAR) |
❌ |
STRING.FIXED.LENGTH(256) |
CHAR(256) |
CHAR(256) |
CHAR(256) |
❌ | CHARACTER(256) |
CHAR(256 CHAR) |
❌ |
STRING.FIXED |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
STRING('max').FIXED |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
STRING(n).NATIONAL |
❌ | NVARCHAR(n) |
NVARCHAR(n) |
❌ | NVARCHAR(n) |
NVARCHAR(n) |
❌ |
STRING(n).FIXED.NATIONAL |
❌ | NCHAR(n) |
NCHAR(n) |
❌ | NCHAR(n) |
NCHAR(n) |
❌ |
STRING(n).INSENSITIVE |
CITEXT1 |
❌ | ❌ | TEXT COLLATE NOCASE1 |
❌ | ❌ | ❌ |
STRING('max').INSENSITIVE |
CITEXT |
❌ | ❌ | TEXT COLLATE NOCASE |
❌ | ❌ | ❌ |
STRING(n).COLLATION('name') |
VARCHAR(n) COLLATE name |
VARCHAR(n) COLLATE name |
VARCHAR(n) COLLATE name |
TEXT COLLATE name |
VARCHAR(n) COLLATE name |
VARCHAR2(n CHAR) COLLATE name |
VARCHAR(n) COLLATE name |
CLOB
These are available in dialects that support Character Large Objects (Sometimes called TEXT, sometimes called CLOB. Equivalent of a BLOB for characters).
| DataType | MySQL/MariaDB | DB2 | Oracle |
|---|---|---|---|
STRING.CLOB('tiny') |
TINYTEXT |
❌ | ❌ |
STRING.CLOB |
TEXT1 |
❌ | CLOB |
STRING(n).CLOB |
TEXT1 |
CLOB(n) |
CLOB1 |
STRING(n).CLOB.NATIONAL |
❌ | NCLOB(n) |
NCLOB1 |
STRING.CLOB('medium') |
MEDIUMTEXT |
❌ | ❌ |
STRING.CLOB('long') |
LONGTEXT |
❌ | ❌ |
MySQL/MariaDB extensions
| DataType | MySQL/MariaDB |
|---|---|
STRING(n).MYSQL_BIN_COLLATION |
VARCHAR(n) BINARY |
STRING(n).FIXED.MYSQL_BIN_COLLATION |
CHAR(n) BINARY |
About NATIONAL
Current mssql implementation uses NVARCHAR by default. In order to not require users to add .NATIONAL everywhere, we could add the following global option:
const sequelize = new Sequelize({
string: {
// Use .NATIONAL by default when using DataTypes.STRING
national: true,
},
});Users can then opt out of using NATIONAL by writing DataTypes.STRING.NATIONAL(false).
We should also add a LENGTH option to allow users to re-use the same STRING type with different lengths:
const NVARCHAR = DataTypes.STRING.NATIONAL;
const User = sequelize.define('User', {
name: NVARCHAR.LENGTH(100),
});BINARY DataType
| DataType | postgres | MySQL/MariaDB | SQL Server | SQLite | DB2 | Oracle | Snowflake |
|---|---|---|---|---|---|---|---|
BINARY |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
BINARY('max') |
BYTEA |
❌ | VARBINARY(MAX) |
BLOB |
❌ | LONG RAW |
VARBINARY |
BINARY(256) |
BYTEA2 |
VARBINARY(256) |
VARBINARY(256) |
BLOB2 |
VARBINARY(256) |
RAW(256) |
VARBINARY(256) |
BINARY(256).FIXED |
❌ | BINARY(256) |
BINARY(256) |
❌ | BINARY(256) |
❌ | ❌ |
BINARY.FIXED |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
BINARY.LENGTH(8000) |
BYTEA2 |
VARBINARY(8000) |
VARBINARY(8000) |
BLOB2 |
VARBINARY(8000) |
RAW(8000) |
VARBINARY(8000) |
While STRING defaults to 255 characters because it's a commonly used type, BINARY requires an explicit length.,
BLOB
These are available in dialects that support Binary Large Objects (BLOB).
| DataType | MySQL/MariaDB | DB2 | Oracle |
|---|---|---|---|
BINARY.BLOB('tiny') |
TINYBLOB |
❌ | ❌ |
BINARY.BLOB |
BLOB |
❌ | BLOB |
BINARY(256).BLOB |
BLOB2 |
BLOB(256) |
BLOB2 |
BINARY.BLOB('medium') |
MEDIUMBLOB |
❌ | ❌ |
BINARY.BLOB('long') |
LONGBLOB |
❌ | ❌ |
BINARY(256).BLOB('long') |
LONGBLOB2 |
❌ | ❌ |
Pending Questions
- Is mssql's
VARCHAR(MAX)more suited toSTRING.CLOBorSTRING('max')? - Do we want to keep
STRING.INSENSITIVE? It's better to use a collation thanCITEXTfor this. Maybe rename toSTRING.CITEXTfor postgres? Or keepCITEXTa separate class?
Footnotes
Reacted by Brandon Brown and functionx.dev
Metadata
Metadata
Assignees
Labels
breaking changeFor issues and PRs. Changes that break compatibility and require a major version increment.
Type
Projects
- StatusShow more project fieldsv8
Activity
[-]Consider deprecating & replacing `DataTypes.STRING`[/-][+][RFC] Consider deprecating & replacing `DataTypes.STRING`[/+]ephys commentedon Sep 30, 2022
Still working on this proposal and after further work on #14505, here is an updated/alternative proposal:
STRING, but mergeCHAR&TEXTwith itSTRINGwould receive new options to support the different types (FIXEDforCHAR,UTF16forNCHAR/NVARCHAR)BINARYtype, removeBLOBandSTRING.BINARYString DataType
STRINGTEXTTEXTVARCHAR(MAX)TEXTCLOB(2147483647)VARCHARSTRING.UTF16NVARCHAR(MAX)STRING(256)VARCHAR(256)VARCHAR(256)VARCHAR(256)TEXTVARCHAR(256)VARCHAR(256)STRING(256).FIXEDCHAR(256)CHAR(256)CHAR(256)CHARACTER(256)STRING(256).FIXED.UTF16NCHAR(256)STRING.FIXEDSTRING('tiny')VARCHAR(256)TINYTEXTVARCHAR(256)TEXTVARCHAR(256)VARCHAR(256)STRING('medium')VARCHAR(2^24)MEDIUMTEXTVARCHAR(MAX)TEXTCLOB(2^24)VARCHAR(2^24)STRING('long')VARCHAR(2^32)LONGTEXTVARCHAR(MAX)TEXTCLOB(2^32)VARCHAR(2^32)STRING('max')TEXTLONGTEXTVARCHAR(MAX)TEXTCLOB(2147483647)VARCHAR(16777216)STRING(8001)VARCHAR(8000)VARCHAR(8000)VARCHAR(MAX)TEXTVARCHAR(8000)VARCHAR(8001)STRING(32705)VARCHAR(32705)VARCHAR(32705)VARCHAR(MAX)TEXTCLOB(32705)VARCHAR(32705)STRING(2^16)VARCHAR(65536)TEXTVARCHAR(MAX)TEXTCLOB(65536)VARCHAR(65536)STRING(2^16+1)VARCHAR(2^16+1)MEDIUMTEXTVARCHAR(MAX)TEXTCLOB(2^16+1)VARCHAR(2^16+1)STRING(2^24+1)VARCHAR(2^24+1)LONGTEXTVARCHAR(MAX)TEXTCLOB(2^32+1)VARCHAR(2^32+1)STRING.COLLATION_BINARYTEXT BINARYTEXT COLLATION BINARYSTRING(n).COLLATION_BINARYVARCHAR(n) BINARYTEXT COLLATION BINARYSTRING(n).FIXED.COLLATION_BINARYCHAR(n) BINARYNote: Dialects that do not support specifying a byte length on the text type could enforce it using a check constraint.
VARCHAR max value of length arg:
Binary DataType
BINARYBYTEABLOBVARBINARY(MAX)BLOBBLOB(2147483647)VARBINARYBINARY(256)BYTEAVARBINARY(256)VARBINARY(256)BLOBVARBINARY(256)VARBINARY(256)BINARY(256).FIXEDBINARY(256)BINARY(256)BINARY(256)BINARY.FIXEDBINARY('tiny')BYTEATINYBLOBVARBINARY(256)BLOBVARBINARY(256)VARBINARY(256)BINARY('medium')BYTEAMEDIUMBLOBVARBINARY(MAX)BLOBBLOB(2^24)VARBINARY(2^24)BINARY('long')BYTEALONGBLOBVARBINARY(MAX)BLOBBLOB(2^32)VARBINARY(2^32)BINARY('max')BYTEALONGBLOBVARBINARY(MAX)BLOBBLOB(2147483647)VARBINARY(16777216)BINARY(8001)BYTEAVARBINARY(8000)VARBINARY(MAX)BLOBVARBINARY(8000)VARBINARY(8001)BINARY(32705)BYTEAVARBINARY(32705)VARBINARY(MAX)BLOBBLOB(32705)VARBINARY(32705)BINARY(2^16)BYTEABLOBVARBINARY(MAX)BLOBBLOB(65536)VARBINARY(65536)BINARY(2^16+1)BYTEAMEDIUMBLOBVARBINARY(MAX)BLOBBLOB(2^16+1)VARBINARY(2^16+1)BINARY(2^24+1)BYTEALONGBLOBVARBINARY(MAX)BLOBBLOB(2^32+1)VARBINARY(2^32+1)Note: Dialects that do not support specifying a byte length on the text type could enforce it using a check constraint.
WikiRik commentedon Sep 30, 2022
Sounds good! I guess we want to do this for v7 but in a future PR, right? Or do you want to tackle this in #14505 as well? (I'd prefer it in a future PR, but I can imagine that doing the same work twice is also not that efficient)
ephys commentedon Sep 30, 2022
Oh no definitely its own PR
ephys commentedon Sep 30, 2022
Another thing is that currently,
STRING.BINARYisVARCHAR BINARYin MySQL/MariaDB. That is not the same thing asVARBINARY. From what I seeVARCHAR BINARYisVARCHARwith a binary collation (utf8mb4_bin).I propose to keep
STRING.BINARY&CHAR.BINARYbut only in MySQL/MariaDB & SQLite (asTEXT COLLATION BINARY). Other dialects should throw instead.I don't like the name, it's ambiguous (as can be seen by the fact that all other dialects have implemented it incorrectly). I propose to deprecate it & rename it to
STRING.BINARY_COLLATION,STRING.FIXED.BINARY_COLLATIONI've updated the table in my previous post
ephys commentedon Sep 30, 2022
In DB2, there is also
(VAR)CHAR FOR BIT DATA, which also doesn't behave likeBINARY(it behaves like(VAR)CHAR. Can't find any documentation on whether it would be correct to assign it toSTRING.COLLATION_BINARY[-][RFC] Consider deprecating & replacing `DataTypes.STRING`[/-][+][RFC] Reworking `DataTypes.STRING`, `DataTypes.CHAR`, `DataTypes.BLOB`, `DataTypes.TEXT`[/+]WikiRik commentedon Nov 24, 2022
@ephys as mentioned during the monthly, I'll try to look into this. Do you think the tables are near completion?
ephys commentedon Nov 26, 2022
In my opinion it's ready, but I'd love a thorough review so we don't need to introduce another breaking change in the future
2 remaining items
[-][RFC] Reworking `DataTypes.STRING`, `DataTypes.CHAR`, `DataTypes.BLOB`, `DataTypes.TEXT`[/-][+][looking for feedback] Reworking `DataTypes.STRING`, `DataTypes.CHAR`, `DataTypes.BLOB`, `DataTypes.TEXT`[/+][-][looking for feedback] Reworking `DataTypes.STRING`, `DataTypes.CHAR`, `DataTypes.BLOB`, `DataTypes.TEXT`[/-][+][looking for feedback] Reworking `DataTypes.STRING`, `DataTypes.CHAR`, `DataTypes.BLOB`, `DataTypes.TEXT`, `DataTypes.CITEXT`[/+][-][looking for feedback] Reworking `DataTypes.STRING`, `DataTypes.CHAR`, `DataTypes.BLOB`, `DataTypes.TEXT`, `DataTypes.CITEXT`[/-][+][looking for feedback] Reworking STRING & BINARY DataTypes[/+]bminer commentedon Dec 20, 2022
My $0.02 is that
VARCHAR(255)should remain the default forDataTypes.STRINGfor a few reasons:TEXThas a slightly larger storage requirement: 2 bytes to represent the string length rather than 1 withVARCHAR(255)(this is true in MySQL, not sure on other DBs).VARCHAR(255)-- People / company names, mailing addresses, phone numbers, emails, etc. Most of these fields are not going to exceed 255 characters.STRING.TEXTis easy enough to tell Sequelize to generate aTEXTfield.There is little justification for changing the current default behavior IMO.
ephys commentedon Dec 21, 2022
Thank you very much for taking the time to give your feedback :)
You raise important issues that indicate that
TEXTis not suitable as a default for MySQL:Note though that
VARCHAR(255)is more often than not going to use a 2 byte length prefix too, due to characters often needing more than one byteI personally dislike the default length of 255 as I think devs should think about which length suits their column. Still, with all the changes we already have, I'm willing to drop this one
Here is yet another design update:
STRINGdefaults toVARCHAR(255)once againSTRING(MAX)for a VARCHAR type with no explicit character limitSTRING(MAX)does not produce a CLOB.TEXT->.CLOB.TEXThas very different meanings depending on the dialect:TEXTin postgres and sqlite is equivalent toVARCHARwhile MySQL's is a CLOB. Also aligns nicely withBINARY.BLOBSTRING.N->STRING.NATIONALBINARYnow requires a length (instead of defaulting to max length)BINARY(MAX)COLLATION_BINARY->MYSQL_BIN_COLLATION, as it's a mysql only optionSTRING.COLLATIONoptionsLatest design
STRING DataType
STRINGVARCHAR(255)VARCHAR(255)VARCHAR(255)TEXT1VARCHAR(255)VARCHAR2(255 CHAR)VARCHAR(255)STRING('max')VARCHARVARCHAR(MAX)TEXTVARCHARSTRING(256)VARCHAR(256)VARCHAR(256)VARCHAR(256)TEXT1VARCHAR(256)VARCHAR(256 CHAR)VARCHAR(256)STRING(256).FIXEDCHAR(256)CHAR(256)CHAR(256)CHARACTER(256)CHAR(256 CHAR)STRING.FIXED.LENGTH(256)CHAR(256)CHAR(256)CHAR(256)CHARACTER(256)CHAR(256 CHAR)STRING.FIXEDSTRING('max').FIXEDSTRING(n).NATIONALNVARCHAR(n)NVARCHAR(n)NVARCHAR(n)NVARCHAR(n)STRING(n).FIXED.NATIONALNCHAR(n)NCHAR(n)NCHAR(n)NCHAR(n)STRING(n).INSENSITIVECITEXT1TEXT COLLATE NOCASE1STRING('max').INSENSITIVECITEXTTEXT COLLATE NOCASESTRING(n).COLLATION('name')VARCHAR(n) COLLATE nameVARCHAR(n) COLLATE nameVARCHAR(n) COLLATE nameTEXT COLLATE nameVARCHAR(n) COLLATE nameVARCHAR2(n CHAR) COLLATE nameVARCHAR(n) COLLATE nameCLOB
These are available in dialects that support Character Large Objects (Sometimes called TEXT, sometimes called CLOB. Equivalent of a BLOB for characters).
STRING.CLOB('tiny')TINYTEXTSTRING.CLOBTEXT1CLOBSTRING(n).CLOBTEXT1CLOB(n)CLOB1STRING(n).CLOB.NATIONALNCLOB(n)NCLOB1STRING.CLOB('medium')MEDIUMTEXTSTRING.CLOB('long')LONGTEXTMySQL/MariaDB extensions
STRING(n).MYSQL_BIN_COLLATIONVARCHAR(n) BINARYSTRING(n).FIXED.MYSQL_BIN_COLLATIONCHAR(n) BINARYAbout NATIONAL
Current mssql implementation uses
NVARCHARby default. In order to not require users to add.NATIONALeverywhere, we could add the following global option:Users can then opt out of using
NATIONALby writingDataTypes.STRING.NATIONAL(false).We should also add a
LENGTHoption to allow users to re-use the sameSTRINGtype with different lengths:BINARY DataType
BINARYBINARY('max')BYTEAVARBINARY(MAX)BLOBLONG RAWVARBINARYBINARY(256)BYTEA2VARBINARY(256)VARBINARY(256)BLOB2VARBINARY(256)RAW(256)VARBINARY(256)BINARY(256).FIXEDBINARY(256)BINARY(256)BINARY(256)BINARY.FIXEDBINARY.LENGTH(8000)BYTEA2VARBINARY(8000)VARBINARY(8000)BLOB2VARBINARY(8000)RAW(8000)VARBINARY(8000)While
STRINGdefaults to 255 characters because it's a commonly used type,BINARYrequires an explicit length.,BLOB
These are available in dialects that support Binary Large Objects (BLOB).
BINARY.BLOB('tiny')TINYBLOBBINARY.BLOBBLOBBLOBBINARY(256).BLOBBLOB2BLOB(256)BLOB2BINARY.BLOB('medium')MEDIUMBLOBBINARY.BLOB('long')LONGBLOBBINARY(256).BLOB('long')LONGBLOB2Pending Questions
VARCHAR(MAX)more suited toSTRING.CLOBorSTRING('max')?STRING.INSENSITIVE? It's better to use a collation thanCITEXTfor this. Maybe rename toSTRING.CITEXTfor postgres? Or keepCITEXTa separate class?Footnotes
String length check constraint ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
Byte Count check constraint ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7
1valdis commentedon Jan 12, 2023
First I was annoyed that
.STRINGwould be 255 by default, then I was comforted that it was changed toTEXTin pg (which I mainly use), but then it was again limited to 255, what's that emotional rollercoaster? 😁I'm used to just use pg's
TEXTeverywhere and rely on validation on the client code. If you're required to increase max text length of something, why bother with migrations and constraints if with TEXT you can simply change client code validation and be done with it? Makes for less confusion between different text types, and also I like its name better thanVARCHAR.Given that, I don't see pg's
TEXTat all in the latest proposal which makes me sad. Even though I know that in pgTEXTandVARCHARwithout length specifier are the same thing, I got used to assuming thatVARCHARis something limited in length andTEXTis unlimited.ephys commentedon Jan 14, 2023
It's a very difficult balancing act between trying to have data types that mean (roughly) the same thing in all dialects, and have good defaults for each dialect. It's a headache.
The design isn't set in stone yet. We're very much still debating this proposal
Note that you can reuse a configured data type like this:
jthoward64 commentedon Jun 16, 2023
One option to balance between having a default length for VARCHAR and encouraging developers to choose a reasonable size would be to make the default a Sequelize constructor option, and if that option is not provided, simple disallow the default constructor. That way not only do you increase the visibility of the fact that
STRINGcolumns have a max length (which some devs miss initially), but also give a bit of extra flexibility. IIRC DataTypes have access to a context object assembled by the Sequelize instance; this value should fit in there.