datamapper/dm-core — Issue #158: Single Table Inheritance problem
Author: mighe
Created: 2011-10-26 08:59:19+00:00
State: open

Short description: adding a constraint in a subclass makes impossible to save a superclass object

Details:

```ruby
class Project
include DataMapper::Resource

property :id,	Serial
property :name,	String, :default => ''
end

class Activity
include DataMapper::Resource

property :id,	Serial
property :name,	String, :default => ''
property :type,	Discriminator
end

project = Project.new(:name => 'project')
puts project.save

activity = Activity.new(:name => 'base activity')
puts activity.save
```

this prints
true
true

now we add a ProjectActivity that extends Activity; every ProjectActivity must reference a Project

```ruby
class Project
include DataMapper::Resource

property :id,	Serial
property :name,	String, :default => ''
end

class Activity
include DataMapper::Resource

property :id,	Serial
property :name,	String, :default => ''
property :type,	Discriminator
end

class ProjectActivity < Activity
belongs_to :project
end

project = Project.new(:name => 'project')
puts project.save

activity = Activity.new(:name => 'base activity')
puts activity.save
```

we get the following exception:
activities.project_id may not be NULL

that's because the ORM uses the Single Table Inheritance and adds a project_id column with NOT NULL constraint: this breaks every save of every class that doesn't have the project_id filed.

Currently I'm using a workaround, setting
belongs_to :project, :required => false
and doing the validation with a method