【问题标题】:How can i fix the constraints and indexes in neo4j for a changed record?如何修复 neo4j 中的约束和索引以更改记录?
【发布时间】:2018-02-18 23:58:02
【问题描述】:

我在 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

导轨 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"

【问题讨论】:

    标签: neo4j neo4j.rb


    【解决方案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
    

    【讨论】:

    • 索引也一样?
    • 添加或删除约束会自动添加/删除关联的索引。您可以添加/删除没有约束的索引,但您不能添加/删除没有索引的约束
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多