Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try:</p> <pre><code>change_column :my_table, :id, :primary_key </code></pre> <p>or</p> <pre><code>my_table.change_column :id, :primary_key </code></pre> <p>Certain Rails database adapters may not let you call <code>change_column</code> on the primary key. If that is the case then you can always call <code>execute</code> to perform the change using SQL directly:</p> <p>MySQL:</p> <pre><code>execute('ALTER TABLE "my_table" CHANGE "id" "id" bigint DEFAULT NULL auto_increment PRIMARY KEY') </code></pre> <p>PostgreSQL (method 1):</p> <pre><code>max_id = execute(%%Q{SELECT id FROM "my_table" ORDER BY "id" DESC LIMIT 1}).to_a.first execute(%%Q{CREATE SEQUENCE "my_table_id_seq" START #{max_id+1}}) execute(%%Q{ALTER TABLE "my_table" ALTER COLUMN "id" TYPE bigint}) execute(%%Q{ALTER TABLE "my_table" ALTER COLUMN "id" SET DEFAULT nextval('my_table_id_seq'::regclass)}) execute(%%Q{ALTER TABLE "my_table" ADD PRIMARY KEY("id")}) </code></pre> <p>PostgreSQL (method 2):</p> <pre><code>max_id = execute(%%Q{SELECT "id" FROM "my_table" ORDER BY "id" DESC LIMIT 1}).to_a.first execute(%%Q{ALTER TABLE "my_table" RENAME COLUMN "id" TO "id_orig"}) execute(%%Q{ALTER TABLE "my_table" ADD COLUMN "id" bigserial NOT NULL}) execute(%%Q{UPDATE "my_table" SET "id"="id_orig"}) execute(%%Q{ALTER SEQUENCE "my_table_id_seq" RESTART #{max_id+1}}) execute(%%Q{ALTER TABLE "my_table" DROP COLUMN "id_orig"}) </code></pre> <p>If you do not want to use <code>bigint</code>/<code>bigserial</code> (64-bit), use <code>int(11)</code>/<code>integer</code>/<code>serial</code> instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload