granite icon indicating copy to clipboard operation
granite copied to clipboard

Can't have a number or underscore in the column name

Open confact opened this issue 4 years ago • 3 comments

I have a model with the column name phone1 from an old rails app.

When I added column phone1 : String? = "" All columns are set to the default value, and it will not get all the data from DB.

Model:

class User < Granite::Base
  table users

  column id : Int32, primary: true
  
  column phone1 : String? = ""
  column first_name : String? = ""
  column last_name : String? = ""
  column email : String? = ""
end  

Log when phone1 is commented

2021-05-26T09:54:02.856692Z  DEBUG - db: Executing query -- query: "SELECT \"users\".\"id\", \"users\".\"first_name\", \"users\".\"last_name\", \"users\".\"email\" FROM \"users\" WHERE id = $1 LIMIT 1", args: [1]
user # => #<User:0x10e268af0 @_current_callback=nil, @errors=[], @new_record=false, @destroyed=false, @id=1, @first_name="first name", @last_name="lastname", @email="email">
2021-05-26T09:54:02.901260Z  DEBUG - granite: [0.34s] SELECT "users"."id", "users"."first_name", "users"."last_name", "users"."email" FROM "users" WHERE id = $1 LIMIT 1: [1]

When I uncomment phone1 column:

2021-05-26T09:57:12.521852Z  DEBUG - db: Executing query -- query: "SELECT \"users\".\"id\", \"users\".\"phone1\", \"users\".\"first_name\", \"users\".\"last_name\", \"users\".\"email\" FROM \"users\" WHERE id = $1 LIMIT 1", args: [1]
user # => #<User:0x111a521e0 @_current_callback=nil, @errors=[], @new_record=false, @destroyed=false, @id=1, @phone1=nil, @first_name="", @last_name="", @email="">
2021-05-26T09:57:12.565476Z  DEBUG - granite: [0.33s] SELECT "users"."id", "users"."phone1", "users"."first_name", "users"."last_name", "users"."email" FROM "users" WHERE id = $1 LIMIT 1: [1]

If I move the phone1 column lower down in the model, the columns above will work fine. So I think it is some serialize issue or macro issue.

confact avatar May 26 '21 10:05 confact

This issue is probably connected or the real issue behind #448

confact avatar May 26 '21 10:05 confact

Seems underscore (_) is not working either. Is it any way to make that work?

confact avatar May 26 '21 10:05 confact

I would like to know the status and if someone could help out fixing this.

I can fix it if I get pointed in the correct direction (file) where the issue might be.

confact avatar Jan 14 '22 17:01 confact

So Granite does underscore column names when they are words and not numbers, I think this issue is focusing on the name ending with a number.

And that should be supported, it makes total sense. It's a common enough pattern, and numbers being present in column names is no big deal.

crimson-knight avatar Apr 25 '23 11:04 crimson-knight

I wasn't able to recreate this using Granite version 0.23.2. @confact is this still an issue?

Here's the full script I used to try to recreate:

require "sqlite3"

Granite::Connections << Granite::Adapter::Sqlite.new(name: "test", url: "sqlite3:./db.sqlite3")

require "granite"
require "granite/adapter/sqlite"

class User < Granite::Base
  connection test
  table users

  column id : Int32, primary: true

  column phone1 : String? = ""
  column first_name : String? = ""
  column last_name : String? = ""
  column email : String? = ""
end

User.migrator.drop_and_create

user = User.create(phone1: "23", first_name: "a", last_name: "5", email: "5")

puts User.find!(user.id).to_h
# => {"id" => 1, "phone1" => "23", "first_name" => "a", "last_name" => "5", "email" => "5"}

stufro avatar Oct 28 '23 09:10 stufro

@stufro I'm not able to reproduce either. I had forgotten about this issue and I have an app in production with a column name with a number and column names with an underscore and everything works fine. I'm using Postgres not SQLite.

I'm going to close this because this appears to be resolved.

crimson-knight avatar Oct 28 '23 15:10 crimson-knight