DataMapper关联,简单代码,无法正常工作

时间:2010-07-01 04:11:14

标签: ruby datamapper

我刚开始使用DataMappers关联,只是为了测试一些非常简单的表结构,而且它是borking。

require 'dm-core'
require 'dm-migrations'

DataMapper.setup( :default, "sqlite3://#{Dir.pwd}/dbtest.db" )

    class Account
      include DataMapper::Resource
      property :id, Serial
      has 1, :userprofile, :model =>"UserProfile"
    end  

    class UserProfile
        include DataMapper::Resource
        property :id, Serial
        belongs_to :Account
    end
DataMapper.finalize #whether I have this before or after auto migrate the result is the same
DataMapper.auto_migrate!

这应该只是工作,美观而简单,DM正在为其他一切工作(我已经在使用它了很多)。

以下是运行此代码时的结果,有人知道问题是什么吗?:

DataObjects::SyntaxError: duplicate column name: account_id (code: 1, sql state: , query: CREATE TABLE "user_profiles" ("id" INTEGER NOT NULL PRIMARY
KEY AUTOINCREMENT, "account_id" INTEGER NOT NULL, "account_id" INTEGER NOT NULL), uri: sqlite3://d/Borrow/dbtest.db)
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/adapters/dm-do-adapter.rb:92:in `execute_non_query'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/adapters/dm-do-adapter.rb:92:in `create_model_storage'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/adapters/dm-do-adapter.rb:90:in `each'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/adapters/dm-do-adapter.rb:90:in `create_model_storage'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-do-adapter-1.0.0/lib/dm-do-adapter/adapter.rb:260:in `with_connection'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/adapters/dm-do-adapter.rb:85:in `create_model_storage'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:79:in `create_model_storage'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:175:in `auto_migrate_up!'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:130:in `auto_migrate!'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:45:in `send'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:45:in `repository_execute'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/model/descendant_set.rb:33:in `each'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/model/descendant_set.rb:33:in `each'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:44:in `repository_execute'
        from d:/Ruby/lib/ruby/gems/1.8/gems/dm-migrations-1.0.0/lib/dm-migrations/auto_migration.rb:22:in `auto_migrate!'

1 个答案:

答案 0 :(得分:3)

UserProfile中,尝试指定小写:account,如下所示:

  belongs_to :account

此外,如果您通过在:user_profile的{​​{1}}关系中添加下划线来遵循约定,

Account

您可以删除 has 1, :user_profile 键。