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 create TIMESTAMP column with MySQL #25830

Closed
PavelPenkov opened this issue Jul 14, 2016 · 4 comments
Closed

Can't create TIMESTAMP column with MySQL #25830

PavelPenkov opened this issue Jul 14, 2016 · 4 comments

Comments

@PavelPenkov
Copy link
Contributor

Steps to reproduce

class AddTSColumn < ActiveRecord::Migration[5.0]
  def change
    add_column :posts, :published_at, 'TIMESTAMP'
  end
end

Expected behavior

A column with TIMESTAMP data type should be created

Actual behavior

A column with DATETIME data type is created

System configuration

Rails version: 5.0.0

Ruby version: 2.3.1p112

@prakashmurthy
Copy link
Contributor

From http://stackoverflow.com/a/3929047/429758

Having said all of this, Rails actually makes some of these decisions for you. Both :timestamp and :datetime will default to DATETIME, while :date and :time corresponds to DATE and TIME, respectively.

This means that within Rails, you only have to decide whether you need to store date, time or both.

tests asserting the same

      def test_datetime_types
        assert_lookup_type :datetime, 'datetime'
        assert_lookup_type :datetime, 'DATETIME'
        assert_lookup_type :datetime, 'timestamp'
        assert_lookup_type :datetime, 'TIMESTAMP'
     end

@sblackstone
Copy link
Contributor

Could you please post a test case that duplicates the issue? I tried to duplicate with this script but the column was created as timestamp..

begin
  require 'bundler/inline'
rescue LoadError => e
  $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
  raise e
end

gemfile(true) do
  source 'https://rubygems.org'
  # Activate the gem you are reporting the issue against.
  gem 'activerecord', '5.0.0'
  gem 'mysql2'
  gem 'byebug'
end

require 'active_record'
require 'minitest/autorun'
require 'logger'

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'r25830', host: 'x.x.x.x', username: 'xxx, password: 'xxx')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
  end

  add_column :posts, :published_at, 'TIMESTAMP'

end

class Post < ActiveRecord::Base
  has_many :comments
end

class BugTest < Minitest::Test
  def test_association_stuff
    assert_equal 1, Post.columns.select {|x| x.sql_type == 'timestamp' }.length 
  end
end

And the SQL becomes:

-- add_column(:posts, :published_at, "TIMESTAMP")
D, [2016-07-14T17:10:30.133512 #41796] DEBUG -- :    (4.9ms)  ALTER TABLE `posts` ADD `published_at` TIMESTAMP

@kamipo
Copy link
Member

kamipo commented Jul 14, 2016

AR aliases small case timestamp to datetime. Thus TIMESTAMP is created as timestamp.
https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L389-L391

But schema dumper does not support timestamp type. #23553 will fix the issue.

@kamipo
Copy link
Member

kamipo commented Feb 23, 2017

Closing since #23553 was merged.

@kamipo kamipo closed this as completed Feb 23, 2017
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

6 participants