How to get around Slick 3.0 schema creation getting errors due to key specs without length

有些话、适合烂在心里 提交于 2019-12-10 11:19:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!