1
Hello, I am facing a problem with Sqlite3. When I run the code:
user = SystemUser.new(email: "[email protected]", password: "marcos123")
user.profile = Profile.new(first_name: "Marcos")
user.save
Rails runs on Sqlite:
(0.1ms) begin transaction
SystemUser Exists (2.7ms) SELECT 1 AS one FROM "system_users" WHERE "system_users"."email" = ? LIMIT ? [["email", "[email protected]"], ["LIMIT", 1]]
SQL (7.1ms) INSERT INTO "system_users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["email", "[email protected]"], ["encrypted_password", "$2a$11$Wl8mzOtFlD9RRCdAr38Ze.vYdrbrPRHkIEqutAsAyodQSDzihy35O"], ["created_at", "2017-11-12 23:27:23.270956"], ["updated_at", "2017-11-12 23:27:23.270956"]]
(0.1ms) begin transaction
SQL (5188.0ms) INSERT INTO "profiles" ("first_name", "created_at", "updated_at", "system_user_id") VALUES (?, ?, ?, ?) [["first_name", "Marcos"], ["created_at", "2017-11-12 23:27:23.281227"], ["updated_at", "2017-11-12 23:27:23.281227"], ["system_user_id", 1]]
(0.1ms) rollback transaction
(6.2ms) rollback transaction
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked: INSERT INTO "profiles" ("first_name", "created_at", "updated_at", "system_user_id") VALUES (?, ?, ?, ?)
from (irb):3
It creates a transaction inside another. I read that Sqlite blocks the file during a transaction. I looked for a resolution but I didn’t find one. If anyone has been through this and can help me I would be very grateful.
I have tested with mysql and the code works
A solution found:
The project was in Rails 5.1.4 I downgraded the project to Rails 5.1.0, the error persisted. After I created a project in Rails 5.0.6, the problem is gone. So that’s it, one solution is to use Rails 5.0.6
Here with 5.0.6
(0.1ms) begin transaction
SQL (10.3ms) INSERT INTO "users" ("email", "created_at", "updated_at") VALUES (?, ?, ?) [["email", "[email protected]"], ["created_at", "2017-11-13 00:27:42.036072"], ["updated_at", "2017-11-13 00:27:42.036072"]]
SQL (0.7ms) INSERT INTO "profiles" ("first_name", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["first_name", "Marcos"], ["user_id", 1], ["created_at", "2017-11-13 00:27:42.122640"], ["updated_at", "2017-11-13 00:27:42.122640"]]
(5.0ms) commit transaction
=> true