Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

change_column produces bad SQL on Postgres #26

Open
jkutner opened this issue May 25, 2011 · 5 comments
Open

change_column produces bad SQL on Postgres #26

jkutner opened this issue May 25, 2011 · 5 comments
Milestone

Comments

@jkutner
Copy link

jkutner commented May 25, 2011

The following migration:

 modify_table :persons do
    change_column :name, String, :length => 255
 end

Produces this invalid SQL on PostgreSQL (version 8.4):

ALTER TABLE "persons" ALTER COLUMN "persons" TYPE "name" VARCHAR(255)

It should produce:

ALTER TABLE "persons" ALTER COLUMN "name" TYPE  VARCHAR(255)

System info:
jruby 1.6.1 (ruby-1.8.7-p330) (2011-04-12 85838f6) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [darwin-x86_64-java]
postgres (PostgreSQL) 8.4.4

@bascht
Copy link

bascht commented Sep 19, 2011

Trapped into the same pit, when I was trying to add an Enum to a class.
Probably related to #30. Steps to reproduce:

require 'dm-core'
require 'dm-types'
require 'dm-migrations'
require 'dm-migrations/migration_runner'

DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, "postgres://user:pass@localhost:5432/mydb")

class Switch
  include DataMapper::Resource
  property :id, Serial
end

DataMapper.auto_migrate!

migration 1, :add_enum_value do
  up do
    modify_table :switches do
      add_column :state, Enum[:off, :on, :broken], :default => :off
    end
  end
end

DataMapper.migrate_up!

results in:

ALTER TABLE "switches" ADD COLUMN "state" INTEGER DEFAULT 'off'

I'd be happy to help. Let me know I can supply more information.

@tlianza
Copy link

tlianza commented Jan 4, 2012

Similar boat here... in my case I actually want to leave the column type the same, but add it as part of the primary key. Seems the syntax doesn't allow that, and I need to re-specify the datatype, which then causes this issue.

@jalcine
Copy link

jalcine commented Jun 3, 2015

I get the same problem here when attempting to increase the length of a String field. Has anyone a patch or suggestions on fixing this?

@jalcine
Copy link

jalcine commented Jun 5, 2015

For now, I've managed to get around this problem by explicitly adding 'type' into the migration statements. For example:

migration 50, :increase_size_of_events_title do
  up do
    modify_table :events do
      change_column :title, 'type varchar', size: 100
    end
  end

  down do
    modify_table :events do
      change_column :title, 'type varchar', size: 50
    end
  end
end

I'd take a look later as to where this statement has to be updated (or if this is something that has to be overwritten). I'm not sure if this is a PostgreSQL specific syntax bug.

@jalcine
Copy link

jalcine commented Jun 6, 2015

So I've noticed a similar problem when attempting to update the falsifiability of the null state of a column, I'd have to do this to have proper SQL generated.

migration 20, :prevent_empty_on_column_address_in_galleries do
  up do
    Site::Models::Gallery.all do |gallery|
      if gallery.address.empty?
        gallery.address = 'New York, New York, United States'
        gallery.save
      end
    end
    modify_table :galleries do
      # NOTE: Use 'set' to change state, no need for type information.
      change_column :address, 'set', allow_nil: false
    end
  end

  down do
    modify_table :galleries do
      # NOTE: Use 'set' to change state, no need for type information.
      change_column :address, 'set', allow_nil: true
    end
  end
end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants