SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] Migration and Column Order

    hi all,

    can migration update the order of the columns? with scaffolding, the order is important b/c the columns display in order. if one wants to migrate in a new column that they want to display inbetween the first and second current columns, can one do it?

    if not, what is recommended? do you need to export out the sql, edit the resulting text file and then reload it? if so, does this screw up migration continuity?

    tia...

  2. #2
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want more control over the views, don't use scaffolding. Just update the views so that the attributes are displayed where you want them, in the order you want them.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skeeterbug View Post
    hi all,

    can migration update the order of the columns? with scaffolding, the order is important b/c the columns display in order. if one wants to migrate in a new column that they want to display inbetween the first and second current columns, can one do it?

    if not, what is recommended? do you need to export out the sql, edit the resulting text file and then reload it? if so, does this screw up migration continuity?

    tia...
    the answer is yes - you just create a migration and delete and recreate columns in the order you want them to be in.

  4. #4
    SitePoint Enthusiast thebasti's Avatar
    Join Date
    Aug 2004
    Location
    Novi Sad, Serbia
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you first delete all columns and then recreate them... Wouldn't that erase all data already in those columns?
    "Strategy without tactics is the slowest route to victory.
    Tactics without strategy is the noise before defeat."
    - Sun Tzu
    Icebergist - a web dev blog | Orange Iceberg Web App Development

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it would defeat the purpose of migrations.

    Don't bind your view to your database. If you need to customise the display order of the columns, the database is *not* the place to do this.

  6. #6
    SitePoint Enthusiast thebasti's Avatar
    Join Date
    Aug 2004
    Location
    Novi Sad, Serbia
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I thought.

    Although skeeterbug's question is still a good one. For example, what if 3rd party program will directly access to the database or if database needs to be exported to CSV and you just need to insert a column in a particular place inside a table. Would this be possible with Rails migrations?
    "Strategy without tactics is the slowest route to victory.
    Tactics without strategy is the noise before defeat."
    - Sun Tzu
    Icebergist - a web dev blog | Orange Iceberg Web App Development

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by thebasti View Post
    That's what I thought.

    Although skeeterbug's question is still a good one. For example, what if 3rd party program will directly access to the database or if database needs to be exported to CSV and you just need to insert a column in a particular place inside a table. Would this be possible with Rails migrations?
    Why would a column in a CSV need to be in a particular place? Why couldn't you just take the CSV into Excel or another program afterwards and rearrange the columns to your liking?

    As for the direct database access idea, that bypasses any logic you have protecting your data, which isn't a good idea. If you're building another app to work with the current app's data, perhaps you should look into all the new REST and ActiveResource stuff in Rails 1.2 to get app #2 hooked into app #1.

  8. #8
    SitePoint Enthusiast thebasti's Avatar
    Join Date
    Aug 2004
    Location
    Novi Sad, Serbia
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was just a hypothetical question. But the conclusion is that you can only add columns at the end of the table.

    As far as I am concerned migrations in Rails are great and extremely useful.
    "Strategy without tactics is the slowest route to victory.
    Tactics without strategy is the noise before defeat."
    - Sun Tzu
    Icebergist - a web dev blog | Orange Iceberg Web App Development

  9. #9
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Migrations don't support (re)ordering columns because most dbs don't support it. MySQL does though, and if you really want to, you can do it by using execute e.g.

    Code:
        def self.up
          execute "ALTER TABLE `table` MODIFY `field1` INT(11) NOT NULL AFTER `field2`"
        end
    You have to give the full column definition though, as it will change it to whatever you pass. The AFTER syntax can be used when you create columns as well, but only via SQL.

  10. #10
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ultimately though, the position of the columns is irrelevant.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •