I am getting an error that I can''t figure out. I have a class called
Test with 3 fields, id, x, and y. When I do
Test.new(''x''=>1,
''y''=>2).save, I get an error saying that id (my
''not null primary
key'') can''t be null. But isn''t active record supposed
to deal with
setting that for me? Here''s the code that''s causing me
trouble:
require ''active_record''
db_name = Time.new.strftime("db/xy-%M%S.sqlite")
# connect to the database (sqlite in this case)
ActiveRecord::Base.establish_connection( "adapter" =>
"sqlite3",
"dbfile" => db_name)
statement = "CREATE TABLE tests (id int not null primary key, x int, y
int)"
begin
ActiveRecord::Base.connection.execute(statement)
rescue
ActiveRecord::StatementInvalid
end
# define a simple model
class Test < ActiveRecord::Base
end
t1 = Test.new(''x''=>1, ''y''=>2)
t1.save
The error I get is:
ActiveRecord::StatementInvalid: tests.id may not be NULL: INSERT INTO
tests (''x'', ''y'') VALUES(1, 2)
...
Lowell
* Lowell Kirsh <lowellk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> [0625 11:25]:> I am getting an error that I can''t figure out. I have a class called > Test with 3 fields, id, x, and y. When I do Test.new(''x''=>1, > ''y''=>2).save, I get an error saying that id (my ''not null primary > key'') can''t be null. But isn''t active record supposed to deal with > setting that for me? Here''s the code that''s causing me trouble: > > > require ''active_record'' > > db_name = Time.new.strftime("db/xy-%M%S.sqlite") > > # connect to the database (sqlite in this case) > ActiveRecord::Base.establish_connection( "adapter" => "sqlite3", > "dbfile" => db_name) > > statement = "CREATE TABLE tests (id int not null primary key, x int, y int)"try statement = "CREATE TABLE tests (id INTEGER PRIMARY KEY, x int, y int)" for starters. if that fails, you might find that AR is puking because the db doesn''t exist before you establish_connection. I didn''t think AR could create one on the fly like that.> begin > ActiveRecord::Base.connection.execute(statement) > rescue > ActiveRecord::StatementInvalid > end > > # define a simple model > class Test < ActiveRecord::Base > end > t1 = Test.new(''x''=>1, ''y''=>2) > t1.save > > > The error I get is: > > ActiveRecord::StatementInvalid: tests.id may not be NULL: INSERT INTO > tests (''x'', ''y'') VALUES(1, 2) > ... > > > Lowell > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- ''Pop a few tea bags in your hot water tank and you can make a hot cuppa anytime by just turning on the tap.'' -- Mrs M Growitt, Birmingham. Rasputin :: Jack of All Trades - Master of Nuns
Perhaps change the id field in the database to be auto_increment or equivalent? hth Paul. --- Lowell Kirsh <lowellk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am getting an error that I can''t figure out. I > have a class called > Test with 3 fields, id, x, and y. When I do > Test.new(''x''=>1, > ''y''=>2).save, I get an error saying that id (my ''not > null primary > key'') can''t be null. But isn''t active record > supposed to deal with > setting that for me? Here''s the code that''s causing > me trouble: > > > require ''active_record'' > > db_name = Time.new.strftime("db/xy-%M%S.sqlite") > > # connect to the database (sqlite in this case) > ActiveRecord::Base.establish_connection( "adapter" > => "sqlite3", > "dbfile" => > db_name) > > statement = "CREATE TABLE tests (id int not null > primary key, x int, y int)" > begin > ActiveRecord::Base.connection.execute(statement) > rescue > ActiveRecord::StatementInvalid > end > > # define a simple model > class Test < ActiveRecord::Base > end > t1 = Test.new(''x''=>1, ''y''=>2) > t1.save > > > The error I get is: > > ActiveRecord::StatementInvalid: tests.id may not be > NULL: INSERT INTO > tests (''x'', ''y'') VALUES(1, 2) > ... > > > Lowell > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >___________________________________________________________ Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com
* Lowell Kirsh <lowellk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> [0612 13:12]:> On 6/29/05, Dick Davies <rasputnik-ogHSZ3ARDZIOXkKaSkYkkl6hYfS7NtTn@public.gmane.org> wrote: > > try > > statement = "CREATE TABLE tests (id INTEGER PRIMARY KEY, x int, y int)" > > Great, it works! I wish sqlite had more error checking built in.yeah, it can be a bit dumb, but that''s what makes it ''lite'' :) the magic incantation ''foo INTEGER PRIMARY KEY'' makes an autoincrement primary key, which is a Good Thing in Rails land. -- ''Avoid parking tickets by leaving your windscreen wipers turned to ''fast wipe'' whenever you leave your car parked illegally.'' -- Top Tips Rasputin :: Jack of All Trades - Master of Nuns