sequelize: EExist: File already exists when using SQLite3
Issue Description
I’m currently just trying to use SQLite 3 with the following,
new Sequelize({
dialect: 'sqlite',
storage: '/path/to/db/db.sqlite'
});
but when I try to sync it, I get the error
EEXIST: file already exists, mkdir '/path/to/db'
Additional context
I found this PR https://github.com/sequelize/sequelize/pull/11853/commits, but I’m not sure it’s where it’s coming from.
jetpack.dir says it’ll throw an error if the path exists but is a file not a directory.
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): sqlite
- I don’t know.
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 25 (9 by maintainers)
here’s solution i found:
new Sequelize({dialect: 'sqlite', storage, dialectOptions: {mode: 2}});
essential part is a
dialectOptions: {mode: 2}
which means read/write access for sqlite3 repo.some other codes, just in case:
The mkdir(Sync) function had no option “recursive” until node.js v10.12.0.
@lakeshadow0 @papb i’m also having this same issue, but only when i use sequelize version ^6.0.0 (if i drop down to 5 then i don’t have the issue)
Thank you very much!!! I think part of the problem was my ignorance about the use of operator
|
to associate multiple modes.But looks like there’s still a problem when I use storage as
:memory:
and mode asSQLite.OPEN_SHAREDCACHE | SQLite.OPEN_READWRITE
. It doesn’t create a shared cache in-memory database. I have no hypothesis of the reason why and if it is at Sequelize or SQLite.An alternative solution that worked for me is to use mode
SQLite.OPEN_URI | SQLite.OPEN_READWRITE
to inform that the storage info is an URI and set storage asfile::memory:?cache=shared
. sqlite uri.The code:
According to sqlite’s docs, you must always use one of the following options:
SQLITE_OPEN_READONLY
SQLITE_OPEN_READWRITE
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
And on top of these you can add extra options such as
SQLITE_OPEN_SHAREDCACHE
So specifying the mode to one of those (with
SQLite
beingimport SQLite from 'sqlite3'
):SQLite.OPEN_SHAREDCACHE | SQLite.OPEN_READWRITE
SQLite.OPEN_SHAREDCACHE | SQLite.OPEN_READONLY
SQLite.OPEN_SHAREDCACHE | SQLite.OPEN_READWRITE | SQLite.OPEN_CREATE
Seems to be what sqlite requires
I’m not quite sure what I did, but the issue isn’t happening anymore. I had to pull the git repo again and after doing another dependency install it resolved. 🤷