Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't get it to work using MySQL #38

Open
LunaSquee opened this issue Aug 2, 2017 · 5 comments
Open

Couldn't get it to work using MySQL #38

LunaSquee opened this issue Aug 2, 2017 · 5 comments

Comments

@LunaSquee
Copy link

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?

@gx0r
Copy link
Owner

gx0r commented Aug 2, 2017

Do you have a stacktrace?

@LunaSquee
Copy link
Author

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

@codeclown
Copy link

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

@codeclown
Copy link

codeclown commented Jan 26, 2018

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 })

@gx0r
Copy link
Owner

gx0r commented Nov 4, 2019

Interesting. I would accept a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants