Philippe Lang
2006-Aug-24 14:48 UTC
[Rails] ActiveRecord, insert and not auto-incremented primary keys...
Hi,
After playing while with ActiveRecord, I''m surprised it isn''t
easier to insert data into a table whose primary key is not auto-incremented by
the database server.
The following code...
---------------------------------------
-- MySQL database "test"
---------------------------------------
CREATE TABLE gardens
(
code integer NOT NULL,
name varchar(50),
PRIMARY KEY (code)
);
---------------------------------------
-- Ruby
---------------------------------------
#!/usr/local/bin/ruby
require ''rubygems''
require_gem ''activerecord''
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:username => "root",
:host => "localhost",
:password => "",
:database => "test"
)
class Garden < ActiveRecord::Base
set_primary_key :code
end
garden = Garden.new("code" => 1, "name" =>
"garden1")
garden.save
---------------------------------------
... gives this error:
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
`log'': Mysql::Error: #23000Duplicate entry ''0'' for
key 1: INSERT INTO gardens (`name`) VALUES(''garden1'')
(ActiveRecord::StatementInvalid)
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/mysql_adapter.rb:184:in
`execute''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/mysql_adapter.rb:194:in
`insert''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1739:in
`create_without_callbacks''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:261:in
`create_without_timestamps''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/timestamp.rb:30:in
`create''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1718:in
`create_or_update_without_callbacks''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:249:in
`create_or_update''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1392:in
`save_without_validation''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/validations.rb:724:in
`save_without_transactions''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in
`save''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
`transaction''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:91:in
`transaction''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:118:in
`transaction''
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in
`save''
from activerecord.rb:24
What is the most elegant way to solve that?
If I remove the primary key configuration, like this:
---------------------------------------
class Garden < ActiveRecord::Base
End
---------------------------------------
... it works.
Is there an equivalent to this?
---------------------------------------
class Garden < ActiveRecord::Base
set_primary_key :code, :manual
End
---------------------------------------
Thanks,
----------------------------------
Philippe Lang, Ing. Dipl. EPFL
Attik System
rte de la Fonderie 2
1700 Fribourg
Switzerland
http://www.attiksystem.ch
Tel: +41 (26) 422 13 75
Fax: +41 (26) 422 13 76
Thomas, Mark - BLS CTR
2006-Aug-24 15:22 UTC
[Rails] Re: ActiveRecord, insert and not auto-incremented primary keys...
Philippe Lang wrote:> class Garden < ActiveRecord::Base > set_primary_key :code > end > > garden = Garden.new("code" => 1, "name" => "garden1") > garden.saveWhen you use set_primary_key, ActiveRecord maps your key to the attribute "id" behind the scenes. So you still use "id". - Mark. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Philippe Lang
2006-Aug-24 16:03 UTC
[Rails] Re: ActiveRecord, insert and not auto-incremented primary keys...
> Philippe Lang wrote:>> class Garden < ActiveRecord::Base >> set_primary_key :code >> end >> >> garden = Garden.new("code" => 1, "name" => "garden1") garden.save > > When you use set_primary_key, ActiveRecord maps your key to > the attribute "id" behind the scenes. So you still use "id". > > - Mark. >Let''s see the problem differently: --------------------------------------- -- MySQL database "test" --------------------------------------- CREATE TABLE incs ( id integer NOT NULL, name varchar(50), PRIMARY KEY (id) ) TYPE = INNODB; --------------------------------------- -- Ruby --------------------------------------- #!/usr/local/bin/ruby require ''rubygems'' require ''active_record'' ActiveRecord::Base.establish_connection( :adapter => "mysql", :username => "root", :host => "localhost", :password => "", :database => "test" ) class Inc < ActiveRecord::Base end i = Inc.new("id" => 123, "name" => "to inc or not to inc?") i.save --------------------------------------- In the incs table, after runnning the code, I let you guess what we find: --------------------------- id name --------------------------- 0 to inc or not to inc? --------------------------- "id" => 123 is not used at all, because it is supposed to be given by the server. --------------- Philippe Lang Attik System
Tom Ward
2006-Aug-24 16:46 UTC
[Rails] Re: ActiveRecord, insert and not auto-incremented primary keys...
The primary key attribute is protected from mass assignment, so can''t
be set by passing its value in a hash. Try this instead:
i = Inc.new("name" => "to inc or not to inc?")
i.id = 123
i.save
Tom
On 24/08/06, Philippe Lang
<philippe.lang-MLEDU+18noL4B7ddeI/ffg@public.gmane.org>
wrote:> > Philippe Lang wrote:
>
> >> class Garden < ActiveRecord::Base
> >> set_primary_key :code
> >> end
> >>
> >> garden = Garden.new("code" => 1, "name"
=> "garden1") garden.save
> >
> > When you use set_primary_key, ActiveRecord maps your key to
> > the attribute "id" behind the scenes. So you still use
"id".
> >
> > - Mark.
> >
>
> Let''s see the problem differently:
>
> ---------------------------------------
> -- MySQL database "test"
> ---------------------------------------
> CREATE TABLE incs
> (
> id integer NOT NULL,
> name varchar(50),
> PRIMARY KEY (id)
> ) TYPE = INNODB;
>
> ---------------------------------------
> -- Ruby
> ---------------------------------------
> #!/usr/local/bin/ruby
> require ''rubygems''
> require ''active_record''
>
> ActiveRecord::Base.establish_connection(
> :adapter => "mysql",
> :username => "root",
> :host => "localhost",
> :password => "",
> :database => "test"
> )
>
> class Inc < ActiveRecord::Base
> end
>
> i = Inc.new("id" => 123, "name" => "to inc
or not to inc?")
> i.save
> ---------------------------------------
>
> In the incs table, after runnning the code, I let you guess what we find:
>
> ---------------------------
> id name
> ---------------------------
> 0 to inc or not to inc?
> ---------------------------
>
> "id" => 123 is not used at all, because it is supposed to be
given by the server.
>
> ---------------
> Philippe Lang
> Attik System
>
>
>
>
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Philippe Lang
2006-Aug-25 05:53 UTC
Re: ActiveRecord, insert and not auto-incremented primary keys...
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org wrote> The primary key attribute is protected from mass assignment, > so can''t be set by passing its value in a hash. Try this instead: > > i = Inc.new("name" => "to inc or not to inc?") i.id = 123 i.save > > TomIt works fine, thanks for your help! Philippe> On 24/08/06, Philippe Lang <philippe.lang-MLEDU+18noL4B7ddeI/ffg@public.gmane.org> wrote: >>> Philippe Lang wrote: >> >>>> class Garden < ActiveRecord::Base >>>> set_primary_key :code >>>> end >>>> >>>> garden = Garden.new("code" => 1, "name" => "garden1") garden.save >>> >>> When you use set_primary_key, ActiveRecord maps your key to the >>> attribute "id" behind the scenes. So you still use "id". >>> >>> - Mark. >>> >> >> Let''s see the problem differently: >> >> --------------------------------------- >> -- MySQL database "test" >> --------------------------------------- >> CREATE TABLE incs >> ( >> id integer NOT NULL, >> name varchar(50), >> PRIMARY KEY (id) >> ) TYPE = INNODB; >> >> --------------------------------------- >> -- Ruby >> --------------------------------------- >> #!/usr/local/bin/ruby >> require ''rubygems'' >> require ''active_record'' >> >> ActiveRecord::Base.establish_connection( >> :adapter => "mysql", >> :username => "root", >> :host => "localhost", >> :password => "", >> :database => "test" >> ) >> >> class Inc < ActiveRecord::Base >> end >> >> i = Inc.new("id" => 123, "name" => "to inc or not to inc?") i.save >> --------------------------------------- >> >> In the incs table, after runnning the code, I let you guess what we >> find: >> >> --------------------------- >> id name >> --------------------------- >> 0 to inc or not to inc? >> --------------------------- >> >> "id" => 123 is not used at all, because it is supposed to be given >> by the server.--------------- Philippe Lang Attik System