Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3735: add_primary_key.diff

File add_primary_key.diff, 1.0 kB (added by nzkoz, 3 years ago)

Patch to implement the functionality. Tests to come once other adapters can confirm the approach is feasible

  • lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    195195 
    196196        execute "CREATE #{index_type} INDEX #{index_name} ON #{table_name} (#{Array(column_name).join(", ")})" 
    197197      end 
     198       
     199      # Adds a primary key to a table.  The column name defaults to 'id'.   
     200      # Only supports integer primary keys, use +execute+ for legacy schema  
     201      # support  
     202      # ===== Example 
     203      #   add_primary_key(:projects_developers) 
     204      #   add_primary_key(:accounts, :acctno) 
     205      def add_primary_key(table_name, column_name='id') 
     206        execute "ALTER TABLE #{table_name} ADD #{column_name} #{native_database_types[:primary_key]}" 
     207      end 
     208       
    198209 
    199210      # Remove the given index from the table. 
    200211      #