Forum: Ruby on Rails create_table with unique combo

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C21ccd7a22dc14980e3cc9cb5cf2dcef?d=identicon&s=25 slava (Guest)
on 2009-04-09 06:42
(Received via mailing list)
I need to create a table replacing default integer id with a string id
and making a combo of (id and version) unique primary key. Here is
what I've got so far.

class CreateCatalogs < ActiveRecord::Migration
  def self.up
    create_table :catalogs, :id => false do |t|
      t.string :id, :limit => 20, :null => false
      t.string :version, :default => '01'

      t.timestamps
    end
  end

  def self.down
    drop_table :catalogs
  end
end
C21ccd7a22dc14980e3cc9cb5cf2dcef?d=identicon&s=25 slava (Guest)
on 2009-04-09 06:57
(Received via mailing list)
alternatively if i leave default integer id as primary key I need to
make sure combo of name and version are unique.

    create_table :catalogs do |t|
      t.string :name
      t.string :version, :default => '01'
      t.timestamps
    end
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-09 07:15
(Received via mailing list)
On Thu, Apr 9, 2009 at 6:56 AM, slava <slava_mikerin@yahoo.com> wrote:
> >
>

To ensure uniqueness, you can add an index

add_index :catalogs, [:name, :version], :unique => true

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
C21ccd7a22dc14980e3cc9cb5cf2dcef?d=identicon&s=25 slava (Guest)
on 2009-04-09 07:16
(Received via mailing list)
on the same subject..
I need to ensure uniqness of name in a table. I tried setting collumn
to unique (see below) but that did not work. I still can create
records with same name field.

   create_table :properties do |t|
      t.string :name, :unique => true
      t.string :description

      t.timestamps
    end
A23a35087d9dba5f1332e5db2fb1b5d2?d=identicon&s=25 Neo (Guest)
on 2009-04-09 08:58
(Received via mailing list)
hi slave,you can do it use the connection method of migration
use it like ActiveRecord::Base.connection_pool.checkout


2009/4/9 slava <slava_mikerin@yahoo.com>

>
>
--
明天会更好
This topic is locked and can not be replied to.