【问题标题】:Neo4j inconsistent behaviour of model classesNeo4j 模型类的行为不一致
【发布时间】:2017-07-05 15:45:49
【问题描述】:

我在我的项目中使用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 函数给出错误,并使用 new 方法返回一个 QueryProxy 对象。 (至于其他模型,它们也像预期的那样正常工作)

以下是在控制台上运行的一些命令:

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 []> 

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

【问题讨论】:

  • 您是否在某个文档页面或博客文章中找到了constraint: :unique?我想删除所有对它的引用
  • 是的...我肯定在博客文章中看到过。再次看到链接后,我会发布链接。
  • @BrianUnderwood,在 neo4j 截屏短系列的第 2 集(属性)中,您已经定义了这个 constraint 事物。参考:youtu.be/2pCSQkHkPC8?t=81
  • 太好了,谢谢!我将添加注释

标签: ruby-on-rails neo4j neo4j.rb


【解决方案1】:

我真的不知道您为什么要为Disease.new 获得QueryProxy。我已经尝试在本地测试您的代码,我得到:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多