Neo4j模型类的行为不一致 [英] Neo4j inconsistent behaviour of model classes
问题描述
我已经在我的项目中使用ActiveNode
模型和neo4j gem创建了5个模型.
I have created 5 models in my project using ActiveNode
model and using neo4j gem.
有一个名为疾病的模型,定义为:
There is a model named Disease, defined as:
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对象. (对于其他模型,它们也可以像预期的那样正常工作)
I am able to create nodes for Factor easily but for Disease, it gives error with create
function and returns a QueryProxy object with new
method. (As for other models, they are also working just as Factor, properly as expected)
以下是在控制台上运行的几个命令:
Here are few commands run on console:
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
节点.尽管有一些警告,但我想知道原因.
So Factor
node is created easily. Although there are couple of warnings and I'd like to know the reason for that.
当我对疾病做同样的事情时:
When I do the same for Disease:
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 []>
对我来说真的很糟糕,对此我也没有任何解决方法.请帮忙!
Its getting really bad for me and I'm not getting any workaround for this. Please help!!
推荐答案
我真的不确定为什么您要为Disease.new
获取QueryProxy
.我尝试在本地测试您的代码,然后得到:
I'm really not sure why you are getting a QueryProxy
for Disease.new
. I've tried testing your code locally and I get:
#<Disease uuid: nil, created_at: nil, disease: nil, factor_effect: nil, updated_at: nil>
也许您的代码中还有其他东西正在影响事情?您可以尝试删除代码,直到代码开始工作为止(尽管那只是黑暗中的一击)
Maybe there is something else in your code that is affecting things? You might try removing code until it starts working (though that's just a stab in the dark)
WARNING
是因为属性不再支持constraint: :unique
.该选项用于在加载模型时自动创建约束,但是这成为维护的噩梦.现在应该使用迁移创建约束.请参阅迁移指南,尤其是本节
The WARNING
s are because constraint: :unique
is no longer supported on properties. That option used to create constraints automatically when the model was loaded, but it became a nightmare to maintain. Constraints should now be created with migrations. See the migration guide, especially this section
这篇关于Neo4j模型类的行为不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!