Re: ActiveRecord uses confusing defaults

Al Evans
Sat, 09 Sep 2006 21:09:37 -0000

Jeroen Houben wrote:
> Al Evans wrote:

>> It has to default to something.
>
> Well in Mysql I *can* set an integer column to NOT NULL and not specify
> a default value. It''s a valid SQL schema. I think AR should copy
this
> behaviour and not force a default zero, but I seem to stand alone in
> this. It''s no biggy really, just wanted to see what the idea
behind this
> AR behaviour was. Thanks!

This is true in a way -- you don''t have to specify a default value.
However, the column will have one anyway. Observe:

mysql> CREATE TABLE `foo` (
    ->   `int1` int(11) NOT NULL,
    ->   `int2` int(11),
    ->   `str1` varchar(255),
    ->   `str2` varchar(255) NOT NULL
    -> )
    -> ;
Query OK, 0 rows affected (0.04 sec)

mysql> show fields from foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| int1  | int(11)      | NO   |     |         |       |
| int2  | int(11)      | YES  |     | NULL    |       |
| str1  | varchar(255) | YES  |     | NULL    |       |
| str2  | varchar(255) | NO   |     |         |       |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> INSERT INTO foo () VALUES();
Query OK, 1 row affected, 2 warnings (0.00 sec)

mysql>  select * from foo;
+------+------+------+------+
| int1 | int2 | str1 | str2 |
+------+------+------+------+
|    0 | NULL | NULL |      |
+------+------+------+------+
1 row in set (0.00 sec)

Even though no default values are set for int1 and str2, int1 is 0 and
str2 is ''''. Those are the default defaults:-)

It has nothing to do with ActiveRecord.

--Al Evans