【问题标题】:Generate datamapper models from an existing database从现有数据库生成数据映射器模型
【发布时间】:2012-05-18 05:05:25
【问题描述】:

datamapper 是否可以从现有数据库模式生成模型?因此,要做迁移的逆操作,它采用模型并生成 sql。我想要的是给定一个数据库模式来生成模型。

【问题讨论】:

    标签: ruby ruby-on-rails-3 orm ruby-datamapper


    【解决方案1】:

    尝试检查https://github.com/yogo/dm-reflection 或其任何分支..

    【讨论】:

    • 感谢@TomMeinIschmidt 的回答。我已经更新了指向 Yogo fork 的答案,它现在是官方存储库。但我没有运气尝试它。 dm-reflection 已经死了。在 Yogo 存储库中,有一个分支应该与 DataMapper 1.0 一起使用,但至少对我来说没有用。它的安装没有用,但我解决了它,但仍然没有用。我认为它有一些破碎的依赖关系。我找到了我现在发布的另一个解决方案。
    【解决方案2】:

    最后,我发现到目前为止最好的解决方案是使用dm-is-reflective plugin: https://github.com/godfat/dm-is-reflective

    它不会为反映现有数据库模式的 DataMapper 模型生成代码,但它的属性访问方法是自动可用的(当然,只要你继续使用这个插件)。

    这是一个使用示例:

    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
    

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 2011-02-23
      相关资源
      最近更新 更多