What is the "proper" way to add constraints using MySQL as the database? I have it set up like this: t.integer :product_id, :null => false, :options=> "CONSTRAINT fk_line_item_products REFERENCES products(id)" This does nothing even if I fill the options with obviously bad syntax or just garbage. Suggestions? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
dev wrote:> What is the "proper" way to add constraints using MySQL as the > database? I have it set up like this: > > t.integer :product_id, :null => false, :options=> "CONSTRAINT > fk_line_item_products REFERENCES products(id)" > > This does nothing even if I fill the options with obviously bad syntax > or just garbage. > > Suggestions?Rails won''t let you do this - it strips the constraint out of the generated sql which i agree is a bit crap. You need to do it directly with a ActiveRecord::Base.connection.execute, or use a plugin/write your own method. There''s a blog post here about the latter: http://soniahamilton.wordpress.com/2008/10/01/ruby-on-rails-foreign-key-constraints-in-mysql/ -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
dev wrote:> What is the "proper" way to add constraints using MySQL as the > database? I have it set up like this: > > t.integer :product_id, :null => false, :options=> "CONSTRAINT > fk_line_item_products REFERENCES products(id)" > > This does nothing even if I fill the options with obviously bad syntax > or just garbage. > > Suggestions?Use a plugin such as matthuhiggins-foreigner or foreign_key_constraints. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Max, The snippet you were trying might work for SQLite but not for mysql. I doubt Rails directly supports the declaration of foreign key constraints through ruby code. So you have take the db specific sql way of adding and executing the foreign key constraints. You can try adding the following to the migration script, to be executed on mysql db. execute "alter table line_items add constraint fk_line_item_products foreign key (product_id) references products(id)" Thanks. On Wed, May 5, 2010 at 5:31 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> dev wrote: > > What is the "proper" way to add constraints using MySQL as the > > database? I have it set up like this: > > > > t.integer :product_id, :null => false, :options=> "CONSTRAINT > > fk_line_item_products REFERENCES products(id)" > > > > This does nothing even if I fill the options with obviously bad syntax > > or just garbage. > > > > Suggestions? > > Use a plugin such as matthuhiggins-foreigner or foreign_key_constraints. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> dev wrote: >> What is the "proper" way to add constraints using MySQL as the >> database? I have it set up like this: >> >> t.integer :product_id, :null => false, :options=> "CONSTRAINT >> fk_line_item_products REFERENCES products(id)" >> >> This does nothing even if I fill the options with obviously bad syntax >> or just garbage. >> >> Suggestions? > > Use a plugin such as matthuhiggins-foreigner or foreign_key_constraints.Sorry, I meant foreign_key_migrations. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Try my fork, sparkfly-foreigner. I had added a much more comprehensive rspec suite to make sure it does what it says it does. http://github.com/sparkfly/foreigner Ho-Sheng Hsiao http://hosheng.blogspot.com/ http://ruby-lambda.blogspot.com/ On May 5, 1:16 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> >> Suggestions? > > > Use a plugin such as matthuhiggins-foreigner or foreign_key_constraints. > > Sorry, I meant foreign_key_migrations.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.