Neo4j 模型的不一致行为 类

Neo4j inconsistent behaviour of model classes

我在我的项目中使用 ActiveNode 模型和使用 neo4j gem.

创建了 5 个模型

有一个名为 Disease 的模型,定义为:

  class Disease
    include Neo4j::ActiveNode
    property :disease, type: String, constraint: :unique
    property :created_at, type: DateTime
    property :updated_at, type: DateTime

    enum factor_effect: [:relief, :worsen]

    # Associations
    has_many :in, :factors, type: :AFFECTED_BY
  end

和因素:

  class Factor
    include Neo4j::ActiveNode
    property :factor, type: String, constraint: :unique
  end

我能够轻松地为 Factor 创建节点,但是对于 Disease,它使用 create 函数和 returns 使用 new 方法的 QueryProxy 对象给出错误。 (至于其他模型,它们也像Factor一样工作,符合预期)

控制台上有几个命令运行:

2.3.3 :012 >   f = Factor.new
=> #<Factor uuid: nil, factor: nil>
2.3.3 :013 > f.factor = "drinking more water"
=> "drinking more water"
2.3.3 :014 > f
=> #<Factor uuid: nil, factor: "drinking more water">
2.3.3 :015 > f.save
HTTP REQUEST: 49ms GET http://localhost:7474/db/data/schema/constraint (0 bytes)
HTTP REQUEST: 8ms GET http://localhost:7474/db/data/schema/index (0 bytes)
WARNING: The constraint option is no longer supported (Defined on Disease for disease)
WARNING: The constraint option is no longer supported (Defined on Factor for factor)
CYPHER CREATE (n:`Factor`) SET n = {props} RETURN n | {:props=>{:uuid=>"33f683d4-a6b2-4c7a-84f9-549088780033", :factor=>"drinking more water"}}
HTTP REQUEST: 682ms POST http://localhost:7474/db/data/transaction (1 bytes)
HTTP REQUEST: 385ms POST http://localhost:7474/db/data/transaction/5/commit (0 bytes)
WARNING: The constraint option is no longer supported (Defined on Disease for disease)
WARNING: The constraint option is no longer supported (Defined on Factor for factor)
=> true
2.3.3 :016 > f
=> #<Factor uuid: "33f683d4-a6b2-4c7a-84f9-549088780033", factor: "drinking more water">

所以Factor节点很容易创建。虽然有几个警告,但我想知道其中的原因。

当我对疾病做同样的事情时:

2.3.3 :020 >   d = Disease.new
WARNING: The constraint option is no longer supported (Defined on Disease for disease)
WARNING: The constraint option is no longer supported (Defined on Factor for factor)
CYPHER
MATCH (result_disease:`Disease`)
RETURN result_disease
HTTP REQUEST: 82ms POST http://localhost:7474/db/data/transaction/commit (1 bytes)
=> #<QueryProxy  []> 
2.3.3 :021 > Disease.all
WARNING: The constraint option is no longer supported (Defined on Disease for disease)
WARNING: The constraint option is no longer supported (Defined on Factor for factor)
    Disease
    MATCH (n:`Disease`)
    RETURN n
    HTTP REQUEST: 11ms POST http://localhost:7474/db/data/transaction/commit (1 bytes)
    => #<QueryProxy Disease []> 

它对我来说真的很糟糕,我没有找到任何解决方法。请帮忙!!

我真的不确定为什么 Disease.new 得到 QueryProxy。我已尝试在本地测试您的代码,我得到:

#<Disease uuid: nil, created_at: nil, disease: nil, factor_effect: nil, updated_at: nil>

也许您的代码中还有其他影响事情的东西?您可以尝试删除代码,直到它开始工作(尽管这只是在黑暗中尝试)

WARNING 是因为属性不再支持 constraint: :unique。该选项用于在加载模型时自动创建约束,但维护起来却成了一场噩梦。现在应该使用迁移创建约束。请参阅迁移指南,尤其是 this section