Custom table name in datamapper

I didn’t know how to do this and it took me a while to find out, so I’m posting it here. Let’s take a look at the following class:

 
class Author

  property :id      , Serial, :key => true
  property :surname , String, :length => 100
  property :initials, String
 
end

And here is the same class, changed to be persisted to a table called my_table_name, of course assuming that the repository in use is default

 
class Author
  
  storage_names[:default] = "my_table_name"
  
  property :id      , Serial, :key => true
  property :surname , String, :length => 100
  property :initials, String
 
end

The storage_names class method is in the class DataMapper::Model, you can find the documentation here. It is also possible to call storage_name= but it doesn’t work… just use storage_name[repository] =

Advertisements

2 comments

  1. trans · March 18, 2010

    Certainly not very intuitive. Most likely you want the table to have the same name no matter the repository. Something nicer would be:

    table_name ‘my_table_name’, :repo => :default

  2. andreadallera · March 18, 2010

    I completely agree. It could be also made that the repository name defaults to :default, so that it is possible to write something like:

    table_name “my_table_name”

    or just

    table “my_table_name”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s