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
Problem with field named timestamp and index with include #51428
Comments
I tested this locally and would like to share my findings. TLDR; I confirmed that trying to load the schema that was generated as described in this issue, causes an error. I updated your test case script to use PostgreSQL and actually add the index: # frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails'
gem 'pg'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# `CREATE DATABASE rails_51428_test;`
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'rails_51428_test')
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Schema.define do
create_table :events, force: true do |t|
t.decimal :amount, precision: 10, scale: 0, default: 0, null: false
t.datetime :timestamp
end
add_index :events, [:amount], name: 'index_events_on_timestamp', include: [:timestamp]
end
ActiveRecord::SchemaDumper.dump
class Event < ActiveRecord::Base; end
class EventTest < Minitest::Test
def test_stuff
assert_equal 0, Event.count
Event.create!(amount: 1)
assert_equal 1, Event.count
# Note: this fails silently. I later discovered that neither this nor
# `ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])`
# fail. See comments below.
puts `rails db:schema:load`
end
end Here is what I get from running it:
While it is clear that the double quotes are added to the schema as you describe, it seems like the tests and
# config/database.yml
development:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: rails_51428_test
class AddIndexIncludingTimestamp < ActiveRecord::Migration[7.1]
def change
add_index :events, [:amount], name: 'index_events_on_timestamp', include: [:timestamp]
end
end
I also tested the PR by @fatkodima (#51429) and it generates the correct schema and works as expected # db/schema.rb
ActiveRecord::Schema[7.2].define(version: 2024_03_26_230735) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "events", force: :cascade do |t|
t.decimal "amount"
t.datetime "timestamp"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["amount"], name: "index_events_on_timestamp", include: ["timestamp"]
end
end |
@danielolivaresd I updated my test case script too, thanks for the update |
Steps to reproduce
When you have a field named "timestamp" and want to add an index including this field, the schema is generated with escape characters, making it impossible to run the load schema
This schema is generated when running the migrate above:
Test case:
I am not sure if the word "timestamp" is reserved for the system, but I started having this problem after upgrading the rails version from 7.0.3 to 7.1.0
Expected behavior
Don't escape the characters at include in index or add this explanation to take care to use field named "timestamp" in Rails Guide.
Actual behavior
Using field named "timestamp" and adding index with this field in include, results in the schema with escaped characters, making it impossible to run the load schema.
System configuration
Rails version: 7.1.0
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered: