Question

Im trying to delete a table and truncate it using the following variations of code. All of the following versions give me the same error.

Code: puts "Clearing Database Table: program_slots" ProgramSlot.destroy_all ActiveRecord::Base.connection.execute("TRUNCATE TABLE program_slots")

   puts "Clearing Database Table: program_slots"
ProgramSlot.destroy_all
ActiveRecord::Base.connection.execute("TRUNCATE TABLE PROGRAMSLOTS")

    puts "Clearing Database Table: program_slots"
ProgramSlot.destroy_all
ActiveRecord::Base.connection.execute("TRUNCATE TABLE 'program_slots'")

    puts "Clearing Database Table: program_slots"
ProgramSlot.destroy_all
ActiveRecord::Base.connection.execute("TRUNCATE TABLE PROGRAM_SLOTS")

The table name is program_slots

The error Im getting is:

  >> ProgramSlot Load (0.6ms)  SELECT `program_slots`.* FROM `program_slots`

     (0.8ms)  TRUNCATE TABLE PROGRAMSLOTS

     Mysql2::Error: Table 'dd_development.programslots' doesn't exist: TRUNCATE TABLE PROGRAMSLOTS

Whats the right syntax? It seems that the underscore I put in my execute statement is not going through .......Notice the error statement is trying to find a table called "programslots" but the real table name is "program_slots".

How do i fix this?

Was it helpful?

Solution

MySQL table names are case sensitive, so that's why #1 and #3 fail. If you're going to use quotes then you need to use the correct ones for MySQL, which is why #2 fails. I'm not sure why this:

ActiveRecord::Base.connection.execute("TRUNCATE TABLE program_slots")

wouldn't work. If it doesn't, you can use this:

ActiveRecord::Base.connection.execute("TRUNCATE TABLE `program_slots`")

Note the angled single quotes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top