unique index fails using ensure_index syntax

47 views
Skip to first unread message

Jon Kern

unread,
Nov 25, 2011, 9:16:09 PM11/25/11
to MongoMapper
Maybe I am missing something (I looked through google as well), but i
find this to be true in one of my older projects:

This method works:

key :email, String, :required => true, :unique => true

This method fails:

User.ensure_index([[:name,1]], :unique => true)

Any ideas???

-- jon
http://technicaldebt.com

----
Full working example here: https://github.com/JonKernPA/mongo_examples/tree/master/indexes

ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10.7.0]

*** LOCAL GEMS ***
bson (1.4.0)
bson_ext (1.4.0)
jnunemaker-validatable (1.8.4)
mongo (1.4.0)
mongo_mapper (0.8.6)
plucky (0.3.8)
etc.

Jon Kern

unread,
Nov 25, 2011, 9:42:13 PM11/25/11
to MongoMapper
I should add that the exact same index is created in both cases.

key :identifier, :required => true
["label_1", #<BSON::OrderedHash:0x829ed7e8 {"ns"=>"md-
test.settings", "unique"=>true, "name"=>"label_1",
"key"=>#<BSON::OrderedHash:0x829ed040 {"label"=>1}>, "v"=>0}>]

key :identifier, :required => true, :unique => true
["label_1", #<BSON::OrderedHash:0x829a72c0 {"ns"=>"md-
test.settings", "unique"=>true, "name"=>"label_1",
"key"=>#<BSON::OrderedHash:0x829a6c1c {"label"=>1}>, "v"=>0}>]

On Nov 25, 9:16 pm, Jon Kern <jonker...@gmail.com> wrote:
> Maybe I am missing something (I looked through google as well), but i
> find this to be true in one of my older projects:
>
> This method works:
>
>     key :email, String, :required => true, :unique => true
>
> This method fails:
>
>     User.ensure_index([[:name,1]], :unique => true)
>
> Any ideas???
>

> -- jonhttp://technicaldebt.com

jnunemaker

unread,
Feb 3, 2012, 4:04:44 PM2/3/12
to mongo...@googlegroups.com
:unique => true validates uniqueness. It doesn't create a unique database index. Pretty sure anyway.


Can you define what you mean by works and doesn't work? Note that you can have issues creating a unique index if you have non-unique data already. You may want to mitigate that prior or drop dups or something.
Reply all
Reply to author
Forward
0 new messages