1

I've run into a problem with my ruby ORM using sqlite3

my orm has a save function that saves a record in the database and sets the objects instance id to the id of the new record:

 def save
    sql = "INSERT INTO #{table_name_for_insert} (#{col_names_for_insert}) VALUES (#{values_for_insert})"
    DB[:conn].execute(sql)
    @id = DB[:conn].execute("SELECT last_insert_rowid() FROM #{table_name_for_insert}")[0][0]
end
  • table_name_for_insert returns "students"
  • col_names_for_insert returns "name, grade"
  • values_for_insert returns "'Sam', '11'"

This all "works" per se, however, when SELECT * FROM students is run after the save method the record returned is:

[{"id" => 1,
'name" => "Sam", 
"grade => 11,
0 => 1, 
1 => "Sam",
2 => 11 }]

I expect the first 3 columns but I have no idea why the last three exist.

If anyone knows whats going on i appreciate it, I'm sure this is a stupid mistake on my part.

lurker
  • 56,987
  • 9
  • 69
  • 103
SVRourke
  • 19
  • 5
  • Can you show your `students` table schema, and show the code that ran the `SELECT * FROM students` and how the result was observed? – lurker Jun 22 '20 at 00:48
  • I used pry after the last line of the method, once in pry i ran DB[:conn].execute("SELECT * FROM students") – SVRourke Jun 22 '20 at 01:56
  • 1
    The driver probably returns the numeric keyed columns. Inspect the query result as you receive it from the driver. – D. SM Jun 22 '20 at 03:25

1 Answers1

0

This was caused by using an improper version of the splite3 gem. After that was fixed I no longer had the issue of the additional columns.

SVRourke
  • 19
  • 5