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_to
relationships are required. I assume you already have Record
entries in your database. The auto_upgrade
is trying to add the new field for the association, and by default it marks that column as NOT NULL
. However, for all the existing records, that value will be NULL.
To get around this, do one of the following:
auto_migrate
instead of auto_upgrade
. This will blow away your data, but
will 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 be NOT NULL
.