relevance / diametric

Diametric is a library for building schemas, queries, and transactions for Datomic from Ruby objects.
MIT License
169 stars 28 forks source link

Cannot save entyity that refereces many other entities #61

Open siemionides opened 10 years ago

siemionides commented 10 years ago

Hello,

I have following models:

    class Badge
      include Diametric::Entity
      include Diametric::Persistence::Peer

      attribute :category, Ref
      attribute :date, DateTime
      attribute :description, String
      attribute :enum_value, String
      attribute :enumeration, String, :cardinality => :many
      attribute :id, UUID
      attribute :name, String
      attribute :type, Ref
      attribute :value, Double
      attribute :week, Integer
      attribute :year, Integer
    end

and

class Person
  include Diametric::Entity
  include Diametric::Persistence::Peer

  attribute :name, String
  attribute :id, UUID, :unique => :identity
  attribute :badges, Ref, :cardinality => :many
  attribute :address, String
  attribute :city, String
  attribute :state, String
  attribute :country, String
  attribute :zip_code, String
  attribute :phone, String
  attribute :email, String
  attribute :age, Integer
  attribute :gender, Ref
end

My intention is to create Person with two Badges.

badge = Badge.new(name: "average_weekly_revenue", value: 30.0, date: DateTime.strptime('2014-10-21T04:05:06', '%Y-%m-%dT%H:%M:%S'))
badge.save

badge2 = Badge.new(name: "some_other_badge", value: 20.0, date: DateTime.strptime('2014-10-21T04:05:06', '%Y-%m-%dT%H:%M:%S'))
badge2.save

so when I create new person:

user_id = UUID.generate
person = Person.new(name: "Natalia Jolie", id: user_id, badges: [badge, badge2])
person.save

I'm being given:

RuntimeError: java.lang.IllegalArgumentException: :db.error/not-an-attribute 17592186045435 is not an attribute.
 get at diametric/DiametricListenableFuture.java:44
save at /Users/michalsiemionczyk/.rvm/gems/jruby-1.7.15/gems/diametric-0.1.3-java/lib/diametric/persistence/peer.rb:15
testD at /Users/michalsiemionczyk/Projects/yeti-backend/michals-test/test.rb:115

And that points to the

 person = Person.new(name: "Natalia Jolie", id: user_id, badges: [badge, badge2])

line. The 17592186045435 number is a dbid of the first badge.

I've tried another approach described in the tutorial in here https://github.com/relevance/diametric#association

user_id = UUID.generate
Person.new(name: "Brad Dipp", id: user_id).save
person = Diametric::Query.new(Person, @conn, true).where(name: "Brad Dipp").first
person.update_attributes(badges: [badge, badge2])
person = Diametric::Query.new(Person, @conn, true).where(name: "Brad Dipp").first

puts person.badges.collect(&:name)

and that throws the same error that points to the person.update_attributes(badges: [badge, badge2]) line.

When I tried the tutorial it throws the same error on me.update_attributes(kids: [mario, luigi]) .

Naturally I'm fine when adding associations with :cardinality => :many, but for my schema I need multiple references.

How to make it work?