Created
August 5, 2009 00:56
-
-
Save snusnu/162407 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'dm-core' | |
DataMapper::Logger.new(STDOUT, :debug) | |
DataMapper.setup(:default, 'sqlite3::memory:') | |
class Person | |
include DataMapper::Resource | |
property :id, Serial | |
has n, :projects | |
has n, :tasks, :through => :projects | |
# everything works fine if specified like this | |
# has n, :tasks, :through => :projects, :via => :tasks | |
end | |
class Project | |
include DataMapper::Resource | |
property :id, Serial | |
belongs_to :person | |
has n, :tasks | |
end | |
class Task | |
include DataMapper::Resource | |
property :id, Serial | |
belongs_to :project | |
end | |
DataMapper.auto_migrate! | |
p '-'*80 | |
# use it when you create a child and have the parent object at hand | |
person = Person.create | |
project = Project.new(:person => person, :tasks => [ Task.new ]) | |
project.save | |
# use it in queries | |
puts Task.all(:project => project).inspect | |
# Have a look at CREATE TABLE projects, it creates a task_id there which definitely isn't right | |
# As a consequence, the creation of a new Project fails because there is no (unneeded) task_id present | |
# ~ (0.000143) SELECT sqlite_version(*) | |
# ~ (0.000105) DROP TABLE IF EXISTS "people" | |
# ~ (0.000017) DROP TABLE IF EXISTS "projects" | |
# ~ (0.000015) DROP TABLE IF EXISTS "tasks" | |
# ~ (0.000024) PRAGMA table_info("people") | |
# ~ (0.000361) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT) | |
# ~ (0.000009) PRAGMA table_info("projects") | |
# ~ (0.000113) CREATE TABLE "projects" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "person_id" INTEGER NOT NULL, "task_id" INTEGER NOT NULL) | |
# ~ (0.000126) CREATE INDEX "index_projects_task" ON "projects" ("task_id") | |
# ~ (0.000096) CREATE INDEX "index_projects_person" ON "projects" ("person_id") | |
# ~ (0.000009) PRAGMA table_info("tasks") | |
# ~ (0.000107) CREATE TABLE "tasks" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "project_id" INTEGER NOT NULL) | |
# ~ (0.000097) CREATE INDEX "index_tasks_project" ON "tasks" ("project_id") | |
# "--------------------------------------------------------------------------------" | |
# ~ (0.000031) INSERT INTO "people" DEFAULT VALUES | |
# ~ projects.task_id may not be NULL (code: 19, sql state: , query: INSERT INTO "projects" ("person_id") VALUES (1), uri: sqlite3://:memory:) | |
# /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:163:in `execute_non_query': projects.task_id may not be NULL (DataObjects::IntegrityError) | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:163:in `execute' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:267:in `with_connection' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:161:in `execute' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:59:in `create' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:32:in `each' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:32:in `create' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/repository.rb:129:in `create' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:698:in `hookable___create_nan_before_advised' | |
# from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.13/lib/extlib/hook.rb:299:in `_create' | |
# from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.13/lib/extlib/hook.rb:297:in `catch' | |
# from /opt/local/lib/ruby/gems/1.8/gems/extlib-0.9.13/lib/extlib/hook.rb:297:in `_create' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:608:in `save_self' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:374:in `save!' | |
# from /opt/local/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/resource.rb:358:in `save' | |
# from dm-features.rb:34 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment