2

私は現在、既存のデータベースに基づいた新しいアプリに取り組んでおり、データ アクセスに DataMapper を利用しています。ただし、外部キーを扱うときの規則は、データベースが使用するものではありません。

例:

class Invoice
  include DataMapper::Resource

  storage_names[:default] = 'invoices'

  property :id, Serial
  # ... more properties ...

  has n, :items
end

class Item
  include DataMapper::Resource

  storage_names[:default] = 'invoiceItems'

  property :id, Serial
  # ... more properties ...

  belongs_to :invoice  # this should use 'invoiceId' instead of 'invoice_id'
end

DataMapper が使用する外部キーを、現在使用しようとしている「invoice_id」ではなく「invoiceId」にする方法はありますか (上記のコメントで示されています)。追加することで通常のフィールドでこれを実行できることは知っていますが:field => 'fieldName'、関連付けのような方法は見つかりませんでした。

4

1 に答える 1

5

初めてではありませんが、Jan De Poorter の非常に役立つブログ エントリのおかげで、私自身の質問に対する答えを見つけました。

フィールド名を扱うときは、アンダースコアに関する組み込みのスマートを使用するのではなく、単にシンボルの名前を使用するように規則を調整する必要があります。

repository(:default).adapter.field_naming_convention = lambda { |value| value.name.to_s }

次に、外部キー名を次のように指定できます。

class Invoice
  # bla bla...
  has n, :items, :child_key => [:invoiceId]
end

class Item
  # bla bla...
  belongs_to :invoice, :child_key => [:invoiceId]
end

(上記のように) 関係の両側でキー名を指定する必要があることに注意してください (これは少し奇妙に思えますが)。うまくいけば、これは同じ質問をしている他の人に役立つでしょう. 答えてくれたJan De Poorterに感謝します。

于 2010-01-01T12:50:53.070 に答える