Is it possible with datamapper to generate the models from an existing database schema? So, to do the inverse of a migration, which takes the models and generate the sql. What I want is given a database schema generate the models.
Asked
Active
Viewed 1,215 times
2 Answers
2
try to check https://github.com/yogo/dm-reflection or any of its forks ..
![](../../users/profiles/877028.webp)
Waiting for Dev...
- 11,486
- 5
- 41
- 56
![](../../users/profiles/1380763.webp)
Tom Meinlschmidt
- 207
- 1
- 3
-
Thanks @TomMeinIschmidt for your answer. I have updated the answer to point to the Yogo fork, which is now the official repository. But I haven't had good luck trying it. `dm-reflection` is quite dead. In Yogo repository there is a branch that is supposed to work with `DataMapper 1.0` but at least for me didn't work. Its installation didn't work but I solved it, but still didn't work. I think it has some broken dependencies. I found another solution I post right now. – Waiting for Dev... May 14 '12 at 11:44
1
At last, I found that till now the best solution is to use dm-is-reflective
plugin: https://github.com/godfat/dm-is-reflective.
It doesn't generate the code for DataMapper models reflecting an existing database schema, but its properties access methods are automatically available (as long as you keep using this plugin, of course).
Here is an example of use:
require 'data_mapper'
require 'dm-is-reflective'
DataMapper.setup(:default, "postgres://user:pwd@localhost/db")
class Table
include DataMapper::Resource
is :reflective #activate dm-is-reflective
reflect #reflects eeach property. You can be more specific (look at plugin documentation)
end
DataMapper.finalize
#Even if no field is defined, all of them are accessible
entry = Table.first(nil, {:id => 469})
print entry.anotherField
![](../../users/profiles/877028.webp)
Waiting for Dev...
- 11,486
- 5
- 41
- 56