In a mysql database I have this table :
CREATE TABLE `actualities` (
`id` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`body` text COLLATE utf8_unicode_ci,
`project_id` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
When I try to create a new entry in this table, the ID is set 0 like this :
mysql> insert into actualities (title) VALUES ('title');
Query OK, 1 row affected, 1 warning (0.13 sec)
mysql> select * from actualities order by created_at desc limit 1;
+----+-------+------+------------+---------------------+---------------------+
| id | title | body | project_id | created_at | updated_at |
+----+-------+------+------------+---------------------+---------------------+
| 0 | title | NULL | NULL | 2012-12-15 20:14:20 | 0000-00-00 00:00:00 |
+----+-------+------+------------+---------------------+---------------------+
I don’t understand why. Do you have a solution?
SOLUTION
ALTER TABLE actualities MODIFY id INT AUTO_INCREMENT;
I guess what you want is this :
Without this, you need to give a specific value when inserting new data in your table or else it will take the default value (here
0)More info on the documentation of MySQL for AUTO_INCREMENT