Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ActiveRecord::StatementInvalid (null value) on rebuild! #2

Closed
zlw opened this issue Sep 27, 2011 · 5 comments
Closed

ActiveRecord::StatementInvalid (null value) on rebuild! #2

zlw opened this issue Sep 27, 2011 · 5 comments

Comments

@zlw
Copy link
Contributor

zlw commented Sep 27, 2011

I've got problem when I'm trying to rebuild hierarchies table. Running User.rebuild! causes this error:
https://gist.github.com/1244628 (tried this on sqlite3 and postgresql)

My model:

class User < ActiveRecord::Base
  # invited_by_id comes from devise_invitable
  acts_as_tree parent_column_name: :invited_by_id
end

Migration file:

class CreateUsersHierarchies < ActiveRecord::Migration
  def change
    create_table :users_hierarchies, id: false do |t|
      t.integer  :ancestor_id, null: false   # ID of the parent/grandparent/great-grandparent/... tag
      t.integer  :descendant_id, null: false # ID of the target tag
      t.integer  :generations, null: false   # Number of generations between the ancestor and the descendant. Parent/child = 1, for example.
    end

    # For "all progeny of..." selects:
    add_index :users_hierarchies, [:ancestor_id, :descendant_id], unique: true

    # For "all ancestors of..." selects
    add_index :users_hierarchies, [:descendant_id]
  end
end

Is this a bug or am I doing something wrong?

@zlw
Copy link
Contributor Author

zlw commented Sep 27, 2011

Ok it must be an issue, because I'm getting the same error when I'm trying to add child:

User.find(1).add_child User.find(2)

Or:

User.find(1).children << User.find(2)

I'm using version 1.0.0 of closure_tree and Rails3.1rc4 (can't update to stable for now)

@mceachen
Copy link
Collaborator

The rebuild shouldn't be trying to insert rows where there isn't an ancestor. I'll switch to postgres and write a test.

@mceachen
Copy link
Collaborator

FWIW, this is the trace using rails 3.1.1 and MySQL:

Tag.find(1).add_child Tag.find(2)
Tag Load (0.9ms) SELECT tags.* FROM tags WHERE tags.id = 1 LIMIT 1
Tag Load (0.7ms) SELECT tags.* FROM tags WHERE tags.id = 2 LIMIT 1
(0.2ms) BEGIN
(0.6ms) UPDATE tags SET parent_id = 1, updated_at = '2011-10-16 02:00:15' WHERE tags.id = 2
(0.2ms) COMMIT
Tag Load (0.3ms) SELECT tags.* FROM tags INNER JOIN tag_hierarchies ON tags.id = tag_hierarchies.ancestor_id WHERE tag_hierarchies.descendant_id = 1 ORDER BY generations asc
(0.3ms) BEGIN
SQL (0.2ms) INSERT INTO tag_hierarchies (ancestor_id, descendant_id, generations) VALUES (1, 2, 1)
(0.2ms) COMMIT

(so that works, at least). I'll try pg now.

@mceachen
Copy link
Collaborator

OK, I just pushed 2.0.0.beta1, and with it:

  • added constraints to the tests to expose the bug.
  • added support for running the tests against PostgreSQL (DB=postgres rake spec)
  • added support for reparenting nodes around in the tree
  • added dependent:destroy and dependent:nullify tests

If you upgrade you should be good. Make sure you call the #rebuild! class method after you upgrade, though.

@zlw
Copy link
Contributor Author

zlw commented Nov 10, 2011

Unfortunately due to this issue I had to change to some other gem in my last project, but I promise I'll try new version and check if it works fine :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants