我在我的项目中使用ActiveNode
模型和neo4j gem创建了5个模型。
有一个名为"疾病"的模型,定义为:
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 创建节点,但对于疾病,它create
函数出错并使用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
。 该选项用于在加载模型时自动创建约束,但维护起来却成了一场噩梦。 现在应该通过迁移创建约束。 请参阅迁移指南,尤其是本节