connect-session-knex
connect-session-knex copied to clipboard
Couldn't get it to work using MySQL
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?
Do you have a stacktrace?
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
I have bumped into this issue right now (with MariaDB). Will try to figure it out.
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 })
Interesting. I would accept a PR.