#58 ✓resolved
Josh H

Specify Table Indexes in Model

Reported by Josh H | November 14th, 2007 @ 03:16 PM

We talked about specifying indexes in the models, which auto_migrate! would use to create indexes on tables during creation/migration. Syntax would be something like:

class Dinosaur < DataMapper::Base
  property :first_name, :string
  property :last_name, :string
  index :last_name, :first_name
end

We'd probably support hash options for index type and index name (by default would be autogenerated).

Comments and changes to this ticket

  • Josh H

    Josh H November 14th, 2007 @ 03:15 PM

    Also, we should support quickie indexes with:

    class Dinosaur < DataMapper::Base
      property :first_name, :string, :index => true
      property :code, :string, :index => :unique
    end
    
  • Administrator

    Administrator December 2nd, 2007 @ 06:45 PM

    Would be nice if single and composite indices were specified the same way.

    • One field can belong to several indices,
    • Each field position in each index can be specified
    • etc.

    Composite indices example:

    class Person < DataMapper::Base

    property :name, String, :index => {1 => {:name => "my_first_idx", :position => 2,

    :type => :unique, :length => 40},

    4 => {:length => 20}

    }

    property :age, Integer, :index => true

    property :height_a, Float, :index => {1 => {:position => 1},

    3 => true,

    4 => true}

    property :height_b, Float, :index => {1 => true,

    3 => true,

    4 => true}

    end

  • Administrator

    Administrator December 2nd, 2007 @ 06:48 PM

    This time with some formatting (hopefully)

    class Person < DataMapper::Base
     property :name, String, :index => {1 => {:name => "my_first_idx", :position => 2,
                                                                            :type => :unique, :length => 40},
                                                                  4 => {:length => 20}
                                                                  }
    property :age, Integer, :index => true
    property :height_a, Float, :index => {1 => {:position => 1}, 3 => true, 4 => true}
    property :height_b, Float, :index => {1 => true, 3 => true, 4 => true}
    end 
    
  • Bjørn Arild Mæland

    Bjørn Arild Mæland December 8th, 2007 @ 10:10 PM

    I liked Josh's proposal so much, that I created a patch for it. I'm a DataMapper noob, but hopefully some of it is ok. A minor change in the proposed syntax tho:

  • Bjørn Arild Mæland

    Bjørn Arild Mæland December 8th, 2007 @ 10:13 PM

    Ok, Lighthouse ate the rest of my comment.. What i was trying to say was that 'index' had to change to 'add_index'. This is because the index method is already defined, and used by ferret methinks. The rest of the syntax is exactly like in Josh's first two posts.

    My patch has two obvious lacks:

    • More specs (I'm kinda at a loss here.. I think :unique and maybe :indexed should be set in get_database_columns in table.rb, and that the DB's meta-queries should be used to get the info. If no-one else is up for it I will try something, but I didn't want to put in too much effort if the patch ended up being rejected)
    • It should be possible to add indexes to a live table, not just upon creation. This probably isn't too hard to implement
  • Bjørn Arild Mæland

    Bjørn Arild Mæland December 11th, 2007 @ 09:36 PM

    • State changed from “new” to “resolved”
    • Milestone cleared.

    I just committed an improved version of my patch in r593

  • Jeffrey Gelens

    Jeffrey Gelens December 12th, 2007 @ 07:43 AM

    I created a patch to add some extra functionality to this new feature. Now you can create a multi column index and specify if it should be unique or not. Example:

     add_index [:server_id, :name, :collected], :unique => true
    

    The old syntax still works, this created 3 keys like it should:

     add_index :server_id, :name, :collected
    

    I added multicol_indexes.diff with the changes.

    I will try to fix validates_uniqueness_of, so that it checks for the duplicated keys.

  • Bjørn Arild Mæland

    Bjørn Arild Mæland December 12th, 2007 @ 10:32 AM

    Hotstuff! Looking at it now. Btw, it would be extra nice if you included specs ;)

  • Bjørn Arild Mæland

    Bjørn Arild Mæland December 12th, 2007 @ 03:48 PM

    Committed a slightly modified version of your patch in r601

  • Jeffrey Gelens

    Jeffrey Gelens December 27th, 2007 @ 09:50 AM

    Ah yeah, sorry. Next patch also the specs, etc. ;D

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Attachments

Tags

Pages