Create a gist now

Instantly share code, notes, and snippets.

dm constraints belongs_to "Can't create table 'dm_constraints.#sql-" bug
require 'bundler/setup'
Bundler.require :default
class List
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :items, constraint: :destroy
end
class Item
include DataMapper::Resource
property :id, Serial
property :name, String
# uncomment this and run again
# belongs_to :list
end
user = "root"
pass = ""
puts `mysql -u #{user} --password=#{pass} -e 'CREATE DATABASE IF NOT EXISTS dm_constraints'`
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup :default, "mysql://#{user}:#{pass}@localhost/dm_constraints"
DataMapper.auto_migrate!
list = List.create( name: "a list" )
items = []
items << list.items.create( name: "item one")
items << list.items.create( name: "item two")
items << list.items.create( name: "item three")
list.destroy
puts Item.all
# error:
# ~ Can't create table 'dm_constraints.#sql-10b_35' (errno: 150) (code: 1005, sql state: HY000, query: ALTER TABLE `items` ADD CONSTRAINT `items_list_fk` FOREIGN KEY (`list_id`) REFERENCES `lists` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, uri: mysql:root:@localhost/dm_constraints)
#/Users/makevoid/.rvm/gems/ruby-1.9.3-p0/bundler/gems/dm-do-adapter-268302022ae8/lib/dm-do-adapter/adapter.rb:63:in `execute_non_query': Can't create table 'dm_constraints.#sql-10b_35' (errno: 150) (DataObjects::SQLError)
# my clue - constraints gem:
# constraints/adapters/do_adapter:71
#
# execute(create_constraints_statement)
def github(gem)
"git@github.com:datamapper/#{gem}"
end
def gh_gem(gem)
gem gem, git: github(gem)
end
%w(dm-core dm-constraints dm-migrations dm-do-adapter dm-mysql-adapter).each do |g|
gh_gem g
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment