InvalidSyntax
InvalidSyntax

Reputation: 9507

Attempt to write to a readonly database error

I have created a new Laravel app utilising SQLite database for user login system. I am experiencing a weird issue where I receive the following error:

> SQLSTATE[HY000]: General error: 8 attempt to write a readonly database
> (SQL: update "users" set "remember_token" =
> HH0dtQYZ5BgoOpya1hNRUrFvIZF0dcYqdIvAjz0k6CbTKBqah7wWPdQbgzzL,
> "updated_at" = 2016-06-18 12:47:43 where "id" = 1)

I initially thought there is some permission issue with my SQLite db file however the correct user has permission to access the file and I have set chmod to 755. The issue ONLY occurs when logging in the first time, if I refresh the page (and resent form data) the app logins in successfully. I do not experience this issue for any other DB actions within my app.

Upvotes: 1

Views: 15252

Answers (3)

Samuel Tosan Ayo
Samuel Tosan Ayo

Reputation: 379

The PDO sqlite requires that you set the directory to read/write permission. To do this add chown www-data before the database path.

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: ' chown www-data ./dev-module/database/database.sqlite',
  syncOnAssociation: true,
});

Upvotes: 0

Samy Massoud
Samy Massoud

Reputation: 4385

The problem, as it turns out, is that the PDO SQLite driver requires that if you are going to do a write operation (INSERT, UPDATE, DELETE, DROP, etc.), then the folder the database resides in must have write permissions, as well as the actual database file.

Upvotes: 5

Xander Luciano
Xander Luciano

Reputation: 3893

For anyone who might need this, check your file permissions.

If your database.sqlite file is under database/, set the permissions to 775 (755 did not work for me).

$ chmod -R 775 database

It may also be worth setting the group to www-data and owner to yourself.

$ chgrp -R www-data database
$ chown -R USERNAME database

obviously replace database with the path to the folder your .sqlite database is. Also check the permissions after any migrations commands, or passport:install. I'm sure it was user error, but at one point I thought I had set file permissions on the directory recursively, but the actual .sqlite file had somehow reverted to 644 and both owner and group were my user.

That may have just been due to me messing up a build script, but my point is - checking a second time is quicker than digging through SO questions.

Also worth noting, if you have some routes working, but others failing with a 500 error, check that you actually have at least 1 row of data in your tables. I would recommend creating seeders and running php artisan passport:install to generate some initial data.

Upvotes: 8

Related Questions