Wednesday, February 08, 2023

ActiveRecord Bug Template

 # frozen_string_literal: true


require "bundler/inline"


gemfile(true) do

  source "https://rubygems.org"


  git_source(:github) { |repo| "https://github.com/#{repo}.git" }


  # Activate the gem you are reporting the issue against.

  gem "activerecord", "~> 6.1.7.2"

  gem "sqlite3"

end


require "active_record"

require "minitest/autorun"

require "logger"


# This connection will do for database-independent bug reports.

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Base.logger = Logger.new(STDOUT)


ActiveRecord::Schema.define do

  create_table :posts, force: true do |t|

    t.string :title, default: "Default Title"

  end

end


class Post < ActiveRecord::Base

end


class BugTest < Minitest::Test

  def test_default_value_null_problem

    post = Post.new

    assert post.save, "Post should be saved without title"

    post.reload

    assert_nil post.title, "Post title should be nil, even with default value specified in migration"

  end

end


Post.connection.execute("INSERT INTO posts (title) VALUES (NULL)")