Skip to content

Instantly share code, notes, and snippets.

@abevoelker
Created January 3, 2015 22:48
Show Gist options
  • Save abevoelker/f6ce3cc2948525ee9a75 to your computer and use it in GitHub Desktop.
Save abevoelker/f6ce3cc2948525ee9a75 to your computer and use it in GitHub Desktop.

How annoying is it that Rails doesn't do what you tell it, but tries to be smart? Trying to use custom DB logic in a Rails migration:

create_table :foos do |t|
  t.timestamp :inserted_time, null: false, default: 'now()'
end

Results in this:

CREATE TABLE foos (
    id integer NOT NULL,
    inserted_time timestamp without time zone DEFAULT '2015-01-03 22:44:59.430585'::timestamp without time zone NOT NULL
);

Having to drop to raw SQL due to avoid the cleverness is annoying.

create_table :foos do |t|
  t.timestamp :inserted_time, null: false
end
execute <<-SQL
  ALTER TABLE foos
    ALTER COLUMN inserted_time SET DEFAULT now()::timestamp;
SQL
CREATE TABLE foos (
    id integer NOT NULL,
    inserted_time timestamp without time zone DEFAULT (now())::timestamp without time zone NOT NULL
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment