Ruby Sequel error, table already exists
Asked Answered
C

1

5

I realized that I can't run the tutorial (http://sequel.jeremyevans.net/) twice.

The first time, it runs as we expect. But the second time, it raises an error

SQLite3::SQLException: table `items` already exists (Sequel::DatabaseError)

I understand the error, but I don't know how to fix it.

How can I open the DB without losing the data it contains, and still being able to run the tutorial?

Cuspid answered 4/9, 2014 at 4:25 Comment(2)
In this case you can take a backup of data and drop the table and create again Table and import your backup dataWormseed
NO! That's terrible! What if you have 10 GB? You can't wait for that twice (when you backup it, and when you import it)Cuspid
C
22

The problem is, it tries to create a DB with the same name, but it already exists, so it raises an error.

Solution:

  • create_table Try to create the table, raise an error if already exists.
  • create_table! Drop the table if exists, and then create the table (YOU LOSE YOUR DATA).
  • create_table? Create the table only if it doesn't exist.
Cuspid answered 4/9, 2014 at 4:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.