Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Sequel error, table already exists

Tags:

sql

ruby

sequel

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?

like image 329
Quarktum Avatar asked Dec 01 '22 01:12

Quarktum


1 Answers

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.
like image 127
Quarktum Avatar answered Dec 06 '22 16:12

Quarktum