class User
include DataMapper::Resource
property :id, Serial
property :name, String
property :email, String
has n, :records
end
class Project
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :records ?????
end
#
class Record
# SPEND_REGEX = /^[0-9]{1}:[0-5]{1}[0-9]{1}$/
include DataMapper::Resource
property :id, Serial
property :reporting_type, String
property :spend_time, String
belongs_to :user
belongs_to :project ????
end
DataMapper.auto_upgrade!
With ??? I marked relation that throws an error “`execute_non_query’: Cannot add a NOT NULL column with default value NULL (DataObjects::SyntaxError)
”
How to define 2 has many relationships to one model in datamapper?
By default, your
belongs_torelationships are required. I assume you already haveRecordentries in your database. Theauto_upgradeis trying to add the new field for the association, and by default it marks that column asNOT NULL. However, for all the existing records, that value will be NULL.To get around this, do one of the following:
auto_migrateinstead ofauto_upgrade. This will blow away your data, butwill allow you to add the relationship columns without it choking on
NULL values.
:required => false. This will allow NULLs in the database. Next, go in and set those fields to the appropriate values. Lastly, modify the database table column to beNOT NULL.