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

Possible issue w/ becomes #16269

Closed
kyledecot opened this issue Jul 23, 2014 · 12 comments
Closed

Possible issue w/ becomes #16269

kyledecot opened this issue Jul 23, 2014 · 12 comments

Comments

@kyledecot
Copy link

In my migration I'm converting my posts table to use uuids instead of the default integers via something like the following:

...
create_table :posts_temp, id: :uuid do |t|
  t.string :title
  ...
end

post_temp_model = Class.new(ActiveRecord::Base) do
  self.table_name = :posts_temp
end

Post.all.each do |post|
  post_temp = post.dup.becomes(post_temp_model)
  binding.pry
  post_temp.title # => "foobar"
  post_temp.save # => true
  post_temp.reload
  post_temp.title # => nil
end
...

When running this and inspecting what post_temp.title is the value appears to be there. When calling post_temp.reload and inspecting the title again however it's nil. It seems as though the value is not being saved to the database.

Is this a bug or am I doing something incorrectly here?

@isaacsanders
Copy link
Contributor

You are not assigning title.

@kyledecot
Copy link
Author

@isaacsanders Sorry, bad code examples. I tried to update my "real" code with the classic Post example. I've updated it to reflect the proper values.

@isaacsanders
Copy link
Contributor

No problem. Helps to know the example is sounds. Thanks!

On Jul 24, 2014, at 7:13 PM, Kyle Decot notifications@github.com wrote:

@isaacsanders Sorry, bad code examples. I tried to update my "real" code with the classic Post example. I've updated it to reflect the proper values.


Reply to this email directly or view it on GitHub.

@senny
Copy link
Member

senny commented Jul 25, 2014

@kyledecot it would be easier if you provide your report in the form of an executable test case. You can use this script as a starting point.

@hmaurer
Copy link
Contributor

hmaurer commented Jul 26, 2014

Are you using postgresql? Here is the test, which currently doesn't pass because afaik SQLite has no built-in support for uuids: https://gist.github.com/a3gis/30bf9b5eee4da8bf3340

@kyledecot
Copy link
Author

@A3gis Yep. The UUID part works find (id gets assigned). It seems like the attributes just get thrown away. @senny I'll get an example with that script up to you soon.

@hmaurer
Copy link
Contributor

hmaurer commented Jul 27, 2014

@kyledecot I gist(ed) an example sript above. The strange thing is that it works fine without the uuid on SQLite3. I'll try it on postgresql.

@bigxiang
Copy link
Contributor

@A3gis I think your gist has a flaw. posts table should have id: :uuid too or the duplicated record couldn't return a correct uuid. It would raise an error when reloading. I can see reload is well on Postgresql after changing it. We probably need to wait for @kyledecot to give an example.

@hmaurer
Copy link
Contributor

hmaurer commented Jul 27, 2014

@bigxiang In the OP: "[...] I'm converting my posts table to use uuids instead of the default integers [...]", so I assume there was no uuid on the posts table?

@bigxiang
Copy link
Contributor

@A3gis, ah I see what you mean. But I really can't execute the gist without uuid on my machine... , if I add it, all assertions are passed... 😞

@rails-bot
Copy link

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@maclover7
Copy link
Contributor

This passes for me on Rails master. I think the issue is that you are using SQLite for your database -- I don't think SQLite supports UUID. I think this should be closed.

require File.expand_path('../../load_paths', __FILE__)

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

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'testy')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, id: :uuid do |t|
    t.string :title
  end

  create_table :posts_temp, id: :uuid do |t|
    t.string :title
  end
end

class Post < ActiveRecord::Base
end

class PostTemp < ActiveRecord::Base
  self.table_name = :posts_temp
end

class BugTest < Minitest::Test
  def test_stuff
    post = Post.create(title: 'foobar')
    post.save

    assert_equal post.title, 'foobar'

    post_temp = post.dup.becomes(PostTemp)

    assert_equal post_temp.title, 'foobar'
    assert_equal post_temp.save, true

    post_temp.reload

    assert_equal post_temp.title, 'foobar'
  end
end

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

8 participants