Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
#
# A one file test to show ...
require 'rubygems'
require 'dm-core'
# setup the logger
DataMapper::Logger.new(STDOUT, :debug)
# connect to the DB
DataMapper.setup(:default, 'sqlite3::memory:')
class Person
include DataMapper::Resource
property :id, Serial
property :email, String, :unique => true
property :thing, Integer
has n, :mailers # A Person sends mailers.
has n, :conversations, :model => "Mailer", :through => Resource # A person receivers mailers.
end
class Mailer
include DataMapper::Resource
property :id, Serial
property :subject, String
property :body, Text
property :sent_at, DateTime
belongs_to :person
has n, :people, :through => Resource
end
DataMapper.auto_migrate!
a = Person.new(:email => "greg@example.com")
a.save
a = Person.new(:email => "stan@example.com", :thing => 1)
a.save
a = Person.new(:email => "jim@example.com", :thing => 1)
a.save
m = Mailer.new()
m.subject = "Subject"
m.body = "Body"
m.person = Person.first() # Greg
m.people = Person.all(:thing => 1)
m.save()
__END__
~ (0.000043) SELECT sqlite_version(*)
~ (0.000062) DROP TABLE IF EXISTS "people"
~ (0.000044) DROP TABLE IF EXISTS "mailers"
~ (0.000013) DROP TABLE IF EXISTS "mailer_people"
~ (0.000012) PRAGMA table_info("people")
~ (0.000335) CREATE TABLE "people" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(50), "thing" INTEGER)
~ (0.000009) PRAGMA table_info("mailers")
~ (0.000174) CREATE TABLE "mailers" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "subject" VARCHAR(50), "body" TEXT, "sent_at" TIMESTAMP, "person_id" INTEGER NOT NULL)
~ (0.000098) CREATE INDEX "index_mailers_person" ON "mailers" ("person_id")
~ (0.000009) PRAGMA table_info("mailer_people")
~ (0.000157) CREATE TABLE "mailer_people" ("person_id" INTEGER NOT NULL, "conversation_id" INTEGER NOT NULL, "mailer_id" INTEGER NOT NULL, PRIMARY KEY("person_id", "conversation_id", "mailer_id"))
~ (0.000039) INSERT INTO "people" ("email") VALUES ('greg@example.com')
~ (0.000069) INSERT INTO "people" ("email", "thing") VALUES ('stan@example.com', 1)
~ (0.000041) INSERT INTO "people" ("email", "thing") VALUES ('jim@example.com', 1)
~ (0.000046) SELECT "id", "email", "thing" FROM "people" ORDER BY "id" LIMIT 1
~ (0.000062) SELECT "id", "email", "thing" FROM "people" WHERE "thing" = 1 ORDER BY "id"
~ (0.000082) INSERT INTO "mailers" ("subject", "body", "person_id") VALUES ('Subject', 'Body', 1)
/Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:162:in `execute_non_query': mailer_people.conversation_id may not be NULL (DataObjects::IntegrityError)
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:162:in `execute'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:266:in `with_connection'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:160:in `execute'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:58:in `create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:31:in `each'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:31:in `create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/repository.rb:125:in `create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:808:in `_create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:621:in `hookable__create_hook_nan_before_advised'
from /Library/Ruby/Gems/1.8/gems/extlib-0.9.13/lib/extlib/hook.rb:299:in `create_hook'
from /Library/Ruby/Gems/1.8/gems/extlib-0.9.13/lib/extlib/hook.rb:297:in `catch'
from /Library/Ruby/Gems/1.8/gems/extlib-0.9.13/lib/extlib/hook.rb:297:in `create_hook'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:554:in `save_self'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:343:in `save'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/model.rb:616:in `send'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/model.rb:616:in `_create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/model.rb:403:in `create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/collection.rb:1061:in `send'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/collection.rb:1061:in `_create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/repository.rb:107:in `scope'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/collection.rb:1061:in `_create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/one_to_many.rb:272:in `_create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/collection.rb:721:in `create'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/many_to_many.rb:416:in `send'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/many_to_many.rb:416:in `create_intermediary'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/many_to_many.rb:397:in `_save'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/relationship.rb:524:in `all?'
from /Library/Ruby/Gems/1.8/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `each'
from /Library/Ruby/Gems/1.8/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `all?'
from /Library/Ruby/Gems/1.8/gems/extlib-0.9.13/lib/extlib/lazy_array.rb:452:in `all?'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/many_to_many.rb:397:in `_save'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/collection.rb:798:in `save'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:584:in `save_children'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/associations/relationship.rb:524:in `all?'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:582:in `each'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:582:in `all?'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:582:in `save_children'
from /Library/Ruby/Gems/1.8/gems/dm-core-0.10.1/lib/dm-core/resource.rb:343:in `save'
from /Users/example/Ruby Workspace/gist-229865/standalone_example.rb:53
~ (0.000193) SELECT "person_id", "conversation_id", "mailer_id" FROM "mailer_people" WHERE "mailer_id" = 1 AND "person_id" = 2 ORDER BY "person_id", "conversation_id", "mailer_id" LIMIT 1
~ mailer_people.conversation_id may not be NULL (code: 19, sql state: , query: INSERT INTO "mailer_people" ("person_id", "mailer_id") VALUES (2, 1), uri: sqlite3://:memory:)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment