datamapper / dm-core

DataMapper - Core
http://datamapper.org/
MIT License
754 stars 153 forks source link

No foreign key after auto_upgrade! #29

Open solnic opened 13 years ago

solnic commented 13 years ago

With the two models.

class Person
  include DataMapper::Resource

  # Primary Key
  property :id, Integer, :key => true

  # References
  has n, :loans, "Ticket", :child_key => [:loaner_id], :constraint => :protect
  has n, :debts, "Ticket", :child_key => [:loanee_id], :constraint => :protect
end

class Ticket
  include DataMapper::Resource

  # Primary Key
  property :id, Serial

  # General Properties
  property :loan_id, UUID, :required => true, :default => lambda { |r, p| UUIDTools::UUID.random_create }, :unique_index => :loan_index
  property :loaner_id, Integer, :required => true, :unique_index => :loan_index
  property :debt_id, UUID, :required => true, :default => lambda { |r, p| UUIDTools::UUID.random_create }, :unique_index => :debt_index
  property :loanee_id, Integer, :required => true, :unique_index => :debt_index
  property :amount, Decimal, :scale => 2, :required => true
  property :started_on, Date, :required => true
  property :expired_on, Date
  property :description, String, :length => 140, :lazy => true
  property :status, Enum[:pending, :active, :cancelled, :closed], :default => :pending, :required => true

  # References
  belongs_to :loaner, "Person"
  belongs_to :loanee, "Person"

  # Validations
  validates_with_block { loaner != loanee }
  validates_with_block(:if => :expired_on) { started_on <= expired_on }
end

auto_upgrade! create no foreign key in database. ( see attachment).


Created by Siegfried Levin - 2011-03-02 05:24:36 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1493

solnic commented 13 years ago

Which foreign key?

by Piotr Solnica (solnic)

solnic commented 13 years ago

loaner_id and loanee_id.

by Siegfried Levin

solnic commented 13 years ago

@Siegfried: Did you use DataMapper.finalize after declaring the models, but before running DataMapper.auto_upgrade! ? It is responsible for inferring the foreign keys and setting them up so that the migration will add them.

by Dan Kubb (dkubb)

solnic commented 13 years ago

@dkubb: I do use DataMapper.finalize.

by Siegfried Levin

nicosalvo commented 12 years ago

I'm experiencing the same problem using datamapper 1.2.0, any help well be appreciated.

ksimard commented 12 years ago

i am using 1.0.2 and i am also having foreign key issues. except my issue is that i added a :required => false flag and it is not dropping the foreign key constraint

i use finalize and auto_upgrade!

dkubb commented 12 years ago

@ksimard auto-upgrading is only additive, this means it only adds new tables, columns, indexes (and foreign keys someday). It will never remove or rename things. In DM2 we may have it make changes that we can determine are "safe", but otherwise in DM1 auto-upgrading will never change something that already exists.