问题
If you take the hello-slick-3.0 typesafe activator template and try to use it with MySQL rather than H2, creating the COFFEES table results in the following MySQL JDCB driver exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: BLOB/TEXT column 'COF_NAME' used in key specification without a key length
This apparently is due to COF_NAME primary key field's using a SQL TEXT column, instead of say, a VARCHAR column, and consequently running into INNODB's limit of 768 bytes for keys. Is there anything that can be done here other than stop using Slicks DDL, and switching to explicit MySQL schema creation?
回答1:
With Slick 3.2, use O.Length
instead of O.Sqltype
:
def name = column[String]("COF_NAME", O.PrimaryKey, O.Length(100))
回答2:
The error happens because MySQL can index only the first N chars of a BLOB or TEXT column. So The error mainly happen when there is a field/column type of TEXT or BLOB or those belongs to TEXT or BLOB types such as TINYBLOB, MEDIUMBLOB, LONGBLOB, TINYTEXT, MEDIUMTEXT, and LONGTEXT that you try to make as primary key or index.(for more info MySQL error: key specification without a key length)
You just need to remove primary key from COF_NAME column:
def name: Rep[String] = column[String]("COF_NAME")
Or you can specify datatype using DBType(dbType: String):
def name: Rep[String] = column[String]("COF_NAME", O.PrimaryKey,O.DBType("varchar(100)"))
回答3:
Since O.DBType is deprecated, it would be more convenient to use O.SqlType:
def name: Rep[String] = column[String]("COF_NAME", O.PrimaryKey,O.Sqltype("varchar(100)"))
来源:https://stackoverflow.com/questions/29573125/how-to-get-around-slick-3-0-schema-creation-getting-errors-due-to-key-specs-with