connect-session-knex icon indicating copy to clipboard operation
connect-session-knex copied to clipboard

Couldn't get it to work using MySQL

Open LunaSquee opened this issue 6 years ago • 5 comments

I keep getting an error ER_TOO_LONG_KEY: Specified key was too long; max key length is 767 bytes when trying to create the required sessions table, any ideas?

LunaSquee avatar Aug 02 '17 09:08 LunaSquee

Do you have a stacktrace?

gx0r avatar Aug 02 '17 21:08 gx0r

No, Unfortunately I didn't save it. But it has something to do with the primary key being too long, I've had this error before with trying to set a varchar as a primary key and I don't know how to solve it

LunaSquee avatar Aug 02 '17 21:08 LunaSquee

I have bumped into this issue right now (with MariaDB). Will try to figure it out.

codeclown avatar Jan 26 '18 16:01 codeclown

OK, so the issue is specific to MySQL, and the error-causing query is this:

alter table `sessions` add primary key `sessions_pkey`(`sid`)
ER_TOO_LONG_KEY:  Specified key was too long; max key length is 767 bytes

The problem is that in certain charsets, such as utf8mb4, the max key length is exceeded (varchar(255) equals to 255*4=1020 bytes).

In "regular" MySQL installations this doesn't happen because MySQL defaults to a 3-byte "utf8" charset (255*3=765).


The way to circumvent this issue right now is to set createtable: false and manually create the table with a shorter sid column.

create table `sessions` (
  `sid` varchar(100) primary key,
  `sess` text not null,
  `expired` datetime not null,
  index (`expired`)
)

Could the default length be shortened in this library, and maybe provide an option to set it manually if necessary? @llambda would you accept a PR?

The default key generation in session seems to result in 32-character keys (but I'm not an expert in this area so that might not be 100% consistent, even though it seems to me that it should always be 32 characters).

In any case it would be helpful to provide this as an option, just in case:

new KnexSessionStore({ sidLength: 50 })

codeclown avatar Jan 26 '18 16:01 codeclown

Interesting. I would accept a PR.

gx0r avatar Nov 04 '19 22:11 gx0r