如何修复neo4j中的约束和索引以更改记录?

时间:2018-02-18 23:58:03

标签: neo4j neo4j.rb

我在neo4j中更改了记录的名称。现在我收到一个错误,暗示有待迁移。

class CreateOrganization < Neo4j::Migrations::Base
  def up
    execute("MATCH (n:Institution) SET n:Organization REMOVE n:Institution RETURN n")
  end

  def down
    execute("MATCH (n:Organization) SET n:Institution REMOVE n:Organization RETURN n")
  end
end

rails 5.1.4

neo4j 3.3.2

当我查看CALL db.constraints时,我可以看到他们仍指向机构。他们的目标是指向组织。

"CONSTRAINT ON ( institution:Institution ) ASSERT institution.uuid IS UNIQUE"

错误看起来像这样......

Neo4j::DeprecatedSchemaDefinitionError in SessionsController#new
Some schema elements were defined by the model (which is no longer 
supported), but they do not exist in the database. Run the following to 
create them if you haven't already: rake 
neo4j:generate_schema_migration[constraint,Organization,uuid] rake 
neo4j:generate_schema_migration[index,Organization,sector] And then run 
`rake neo4j:migrate` (zshell users may need to escape the brackets)

当我跑步时

rake neo4j:generate_schema_migration[constraint,Organization,uuid]

我得到了

zsh: no matches found: neo4j:generate_schema_migration[constraint,Organization,uuid]

更新:在我的答案中创建了Brian提供的迁移之后,与约束相关的错误部分消失了。但是,与索引相关的部分错误仍然存​​在。我试图使用帮助器来添加和删除模型中的索引。

class AddIndexToOrganization < Neo4j::Migrations::Base
  def up
    add_index :Organization, :uuid
    drop_index :Institution, :uuid
  end

  def down
    drop_index :Organization, :uuid
    add_index :Institution, :uuid
  end
end

然后我尝试运行迁移。这引发了错误:

== 20180224004338 AddIndexToOrganization: running... 
===========================
rake aborted!
Neo4j::MigrationError: Duplicate index for Organization#uuid

有趣的是,当我使用CALL db.indexes时,我找不到关于组织的索引,这仍然是"INDEX ON :Institution(sector)" "Institution"

1 个答案:

答案 0 :(得分:1)

运行MATCH (n:Institution) SET n:Organization REMOVE n:Institution RETURN n不会更改约束,只会更改标签。约束仍然是旧标签的设置。您应该能够在新迁移中使用drop_constraintadd_constraint帮助程序(请参阅docs page了解迁移帮助程序)。它应该看起来像:

class CreateOrganization < Neo4j::Migrations::Base
  def up
    add_constraint(:Organization, :uuid)
    remove_constraint(:Institution, :uuid)
  end

  def down
    remove_constraint(:Organization, :uuid)
    add_constraint(:Institution, :uuid)
  end
end