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.

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

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.