# Fields in the wrong order when saving to sqllite3

**URL:** <https://www.ruby-forum.com/t/fields-in-the-wrong-order-when-saving-to-sqllite3/130690>\
**Category:** Rails\
**Created:** [March 1, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/fields-in-the-wrong-order-when-saving-to-sqllite3/130690 "2008-03-01T22:42:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 1, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/fields-in-the-wrong-order-when-saving-to-sqllite3/130690/1 "2008-03-01T22:42:22Z")

</div>

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.”

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 3, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/fields-in-the-wrong-order-when-saving-to-sqllite3/130690/2 "2008-03-03T22:30:36Z")

</div>

Hi,

Indeed the gender column was the problem. Thanks for all the help!

/Fredrik

On Sun, Mar 2, 2008 at 3:31 PM, Mark B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You have a NULL going into the “gender” column, however:  
> class Member \< ActiveRecord::Base
> 
> # some code for success
> 
> else
> 
> # check object.errors for problems
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
“Give up learning, and put an end to your troubles.”

---

<div class="post-metadata">

**Author:** ![Fredrik\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@Fredrik\_K](https://www.ruby-forum.com/u/Fredrik_K)\
**Post date:** [March 2, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/fields-in-the-wrong-order-when-saving-to-sqllite3/130690/3 "2008-03-02T15:31:25Z")

</div>

Fredrik K. 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
