#169 ✓resolved
Tim Morgan

table.create! does not create columns when called in a different database context

Reported by Tim Morgan | February 26th, 2008 @ 02:29 AM

If you define a DataMapper::Base instance in the context of one database, then call its table.create! method in the context of another database, it will create the table in that second context, but will not create any columns.

Here is complete Ruby code to reproduce the problem, when using PostgreSQL:

require 'rubygems'
require 'data_mapper'

# Create two databases
`createdb first`
`createdb second`

# Define the context for the first database
DataMapper::Database.setup(:first, {
  :adapter => 'postgresql',
  :host => 'localhost',
  :username => 'postgres',
  :password => 'pass',
  :database => 'first'
})

# Define the context for the second database
DataMapper::Database.setup(:second, {
  :adapter => 'postgresql',
  :host => 'localhost',
  :username => 'postgres',
  :password => 'pass',
  :database => 'second'
})

# In the context of the first database...
DataMapper.database(:first) do |context|
  # Initialize an ORM object
  class Example < DataMapper::Base
    property :name, :string
  end
end

# In the context of the second database...
DataMapper.database(:second) do |context|
  # Create the example table
  Example.table.create!
end

# Now print the columns for the example table
puts `echo "\\d examples" | psql second`

sleep 4

# And drop the example databases
`dropdb first`
`dropdb second`

I have not tested this in MySQL or SQLite. It should output an "examples" table with no columns save for the primary key, even though a "name" column should also exist.

I'm writing this under the assumption that you can define ORM objects in one context and use them in any context. Even if that's not the case -- if it's the case that if you define an ORM object in one context you are limited to using it in that context -- it should still fail more explicitly than this.

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Tags

Pages