-
|
Following is a single file script to reproduce this behavior: require "pg"
require "avram"
class AppDatabase < Avram::Database
end
AppDatabase.configure do |settings|
settings.credentials = Avram::Credentials.new(database: "test_db", username: "postgres", hostname: "localhost")
end
Avram.configure do |settings|
settings.database_to_migrate = AppDatabase
settings.lazy_load_enabled = true
end
class CreateUserAudit::V20250315162146 < Avram::Migrator::Migration::V1
def migrate
create table_for(UserAudit) do
primary_key id : Int64
add_timestamps
add user_id : Int64
add changed_column_name : String
add from : String
add to : String
end
create_index table_for(UserAudit), [:user_id, :changed_column_name]
end
def rollback
drop table_for(UserAudit)
end
end
Avram::Migrator::Runner.drop_db(true)
Avram::Migrator::Runner.create_db(true)
Avram::Migrator::Runner.new.run_next_migration
abstract class BaseModel < Avram::Model
def self.database : Avram::Database.class
AppDatabase
end
end
class UserAudit < BaseModel
table do
column user_id : Int64
column changed_column_name : String
column from : String
column to : String
end
end
UserAudit::SaveOperation.create!(user_id: 1_i64, changed_column_name: "column_name", from: "", to: "foo")When run above code, I get error like this: I have no idea why this happen, AFAIK, the TEXT type column in postgresql can save a blank string, right? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
This is correct behavior. Avram (and Lucky) treat empty strings the same as nil. This is because an empty string on a non-nilable field will usually lead to bugs. For example, imagine displaying a user's name on a page, but the name is an empty string. It might say I have an issue to document this luckyframework/website#1288 but I've just never got around to doing it 😂 You can do this: table do
column user_id : Int64
column changed_column_name : String
column from : String, allow_blank: true
column to : String
end |
Beta Was this translation helpful? Give feedback.
This is correct behavior. Avram (and Lucky) treat empty strings the same as nil. This is because an empty string on a non-nilable field will usually lead to bugs. For example, imagine displaying a user's name on a page, but the name is an empty string. It might say
" , your report is now ready".I have an issue to document this luckyframework/website#1288 but I've just never got around to doing it 😂
You can do this: