Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
confact opened this issue May 26, 2021 · 6 comments
Closed

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

confact opened this issue May 26, 2021 · 6 comments

Comments

@confact
Copy link

confact commented May 26, 2021

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
Copy link
Author

confact commented May 26, 2021

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

@confact
Copy link
Author

confact commented May 26, 2021

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

@confact confact changed the title Can't have a number in the column name Can't have a number or underscore in the column name May 26, 2021
@confact
Copy link
Author

confact commented Jan 14, 2022

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.

@crimson-knight
Copy link
Member

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.

@stufro
Copy link
Contributor

stufro commented Oct 28, 2023

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"}

@crimson-knight
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants