Hmm, that is not how I thought primary keys worked in SQL. I guess I
was right then, there definitely is an easier way to do it (hehe)

On Mar 5, 11:30 pm, Xavier Shay <[email protected]> wrote:
> > but this seems like it would solve your
> > problem. You basically don't want a MessageDigest to be created if one
> > with the same code exists for a specific data_file_id
>
> Close but broken in a concurrent environment. Always try and fix these
> problems in the data model first.
>
> If you include the data_file_id as a property you can make it part of
> the key
>
> class MessageDigest
>   include DataMapper::Resource
>   property :data_file_id, Integer, :key => true
>   property :code, Enum[:md5, :sha1, :crc32], :key => true
>   property :value,  String, :required => true, :length => 255
>
>   belongs_to :data_file
>
>   validate_is_unique :data_file_id # I don't think you get this auto-
> magically
> end
>
> On Feb 18, 2:01 pm, casual <[email protected]> wrote:
>
>
>
> > I feel like there is an easier way to do this, like :unique
> > => :key_uniqueness_is_based_on
>
> > class DataFile
> >   include DataMapper::Resource
> >   property :id, Serial
> >   property :contents, Text
> >   has n, :message_digests
> > end
>
> > class MessageDigest
> >   include DataMapper::Resource
> >   property :code, Enum[:md5, :sha1, :crc32], :key => true
> >   property :value,  String, :required => true, :length => 255
> >   belongs_to :data_file
> >   before :create, :check_unique_code
>
> >   protected
> >   def check_unique_code
> >     MessageDigest.first(:code => code, :data_file_id => data_file_id)
> >   end
> > end
>
> > On Feb 16, 1:33 pm, franco <[email protected]> wrote:
>
> > > I want a DataFile to have at most one MessageDigest of a certain code.
> > > I've tried a bunch of combinations of :key and :unique_key etc but i
> > > cant seem to get it right.
>
> > > if i were to do this in a SQL DDL i'd make data_file_id and code a
> > > composite key. how can i make it work in datamapper?
>
> > > example models:
>
> > > class DataFile
> > >   include DataMapper::Resource
> > >   property :id, Serial, :key => true
> > >   property :contents, Text
> > >   has 0..n, :message_digests
> > > end
>
> > > class MessageDigest
> > >   include DataMapper::Resource
> > >   property :code, Enum[:md5, :sha1, :crc32]
> > >   property :value,  String, :required => true, :length => 255
> > >   belongs_to :data_file
> > > end

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to