Hi, New to rails, and trying to make my first project work. When I try to save a new record to the sqllite database, I see that the SQL tries to insert the fields in the wrong order: ActiveRecord::StatementInvalid in MembersController#create SQLite3::SQLException: SQL logic error or missing database: INSERT INTO members ("secondary_city", "primary_adress", "updated_at", "primary_email", "primary_mobile", "secondary_telephone", "club_id", "secondary_zip", "gender", "is_active", "secondary_adress", "given_name", "secondary_email", "secondary_mobile", "primary_telephone", "date_of_birth", "primary_city", "primary_zip", "surname", "created_at") VALUES('''', '''', ''2008-03-01 22:35:30'', NULL, '''', '''', 1, '''', NULL, ''t'', ''fdsdsf'', ''Fredrik'', NULL, '''', '''', ''2008-03-01'', '''', '''', ''Karlsson'', ''2008-03-01 22:35:30'') db zak$ sqlite3 development.sqlite3 SQLite version 3.4.0 Enter ".help" for instructions sqlite> .schema members CREATE TABLE members ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "given_name" varchar(255) NOT NULL, "surname" varchar(255) NOT NULL, "date_of_birth" date DEFAULT NULL, "gender" integer NOT NULL, "primary_adress" varchar(255) DEFAULT NULL, "primary_zip" varchar(255) DEFAULT NULL, "primary_city" varchar(255) DEFAULT NULL, "primary_email" varchar(255) DEFAULT NULL, "primary_telephone" varchar(255) DEFAULT NULL, "primary_mobile" varchar(255) DEFAULT NULL, "secondary_adress" varchar(255) DEFAULT NULL, "secondary_zip" varchar(255) DEFAULT NULL, "secondary_city" varchar(255) DEFAULT NULL, "secondary_email" varchar(255) DEFAULT NULL, "secondary_telephone" varchar(255) DEFAULT NULL, "secondary_mobile" varchar(255) DEFAULT NULL, "is_active" boolean DEFAULT ''t'', "club_id" integer NOT NULL, "created_at" datetime DEFAULT NULL, "updated_at" datetime DEFAULT NULL); How come the order of the fields are all wrong? I''m on 2.0.2. /Fredrik -- "Give up learning, and put an end to your troubles." --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Fredrik Karlsson wrote:> New to rails, and trying to make my first project work. When I try to > save a new record to the sqllite database, I see that the SQL tries to > insert the fields in the wrong order:SQL syntax allows you to say: INSERT INTO my_table (COLUMN_5, COLUMN_2, COLUMN_8) VALUES (col5_value, col2_value, col8_value) As long as the order in the column name list matches the order of the values, the correct values will be placed in the correct columns. In your example you have:> "secondary_zip", "gender", "is_active", "secondary_adress",> '''', '''', 1, '''', NULL, ''t'', ''fdsdsf'', ''Fredrik'', NULL, '''', '''',You have a NULL going into the "gender" column, however:> "date_of_birth" date DEFAULT NULL, "gender" integer NOT NULL,The "gender" column doesn''t allow NULL. For each column with a NOT NULL constraint in your database, add a "validates_presence_of" call to your model. For example, in this case, in your Member class: class Member < ActiveRecord::Base validates_presence_of :gender This will mean the model itself will ensure it only tries to save objects which meet the database constraints. If you are using standard forms for adding the data with scaffolded layouts, then you will see the errors and which fields they relate to. Otherwise, after you call #save on your object, if you get "false" then you can check the errors on the object: if object.save # some code for success else # check object.errors for problems -- 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-/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?hl=en -~----------~----~----~----~------~----~------~--~---
Fredrik Karlsson
2008-Mar-03 21:27 UTC
Re: Fields in the wrong order when saving to sqllite3
Hi, Indeed the gender column was the problem. Thanks for all the help! /Fredrik On Sun, Mar 2, 2008 at 3:31 PM, Mark Bush <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Fredrik Karlsson wrote: > > New to rails, and trying to make my first project work. When I try to > > save a new record to the sqllite database, I see that the SQL tries to > > insert the fields in the wrong order: > > SQL syntax allows you to say: > > INSERT INTO my_table (COLUMN_5, COLUMN_2, COLUMN_8) > VALUES (col5_value, col2_value, col8_value) > > As long as the order in the column name list matches the order of the > values, the correct values will be placed in the correct columns. > > In your example you have: > > > "secondary_zip", "gender", "is_active", "secondary_adress", > > > > '''', '''', 1, '''', NULL, ''t'', ''fdsdsf'', ''Fredrik'', NULL, '''', '''', > > You have a NULL going into the "gender" column, however: > > > > "date_of_birth" date DEFAULT NULL, "gender" integer NOT NULL, > > The "gender" column doesn''t allow NULL. > > For each column with a NOT NULL constraint in your database, add a > "validates_presence_of" call to your model. For example, in this case, > in your Member class: > > class Member < ActiveRecord::Base > validates_presence_of :gender > > This will mean the model itself will ensure it only tries to save > objects which meet the database constraints. If you are using standard > forms for adding the data with scaffolded layouts, then you will see the > errors and which fields they relate to. Otherwise, after you call #save > on your object, if you get "false" then you can check the errors on the > object: > > if object.save > # some code for success > else > # check object.errors for problems > > > -- > Posted via http://www.ruby-forum.com/. > > > >-- "Give up learning, and put an end to your troubles." --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---