1

I've inserted many rows into tables before in the past but this particular instance keeps giving me an error and I can't figure out why. I've restarted mysql/tried disabling foreign key checks and etc but it still fails.

Here is my insert command:

insert into subreddits_subreddit(name, desc, admin_id) values('firstSubreddit', 
      'This is a test.', 1)

My error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that c   
orresponds to your MySQL server version for the right syntax to use near 'desc, admin_id) 
values('firstSubreddit', 'This is a test.', 1)' at line 1

Here is the schema for subreddits_subreddit.

subreddits_subreddit | CREATE TABLE `subreddits_subreddit` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `desc` varchar(3000) DEFAULT NULL,
  `admin_id` int(11) DEFAULT NULL,
  `created_on` datetime DEFAULT NULL,
  `updated_on` datetime DEFAULT NULL,
  `status` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `user_id` (`admin_id`),
  CONSTRAINT `subreddits_subreddit_ibfk_1` FOREIGN KEY (`admin_id`) REFERENCES 
          `users_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

Here is the users table which it references:

 users_user | CREATE TABLE `users_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(80) DEFAULT NULL,
  `email` varchar(200) DEFAULT NULL,
  `password` varchar(200) DEFAULT NULL,
  `created_on` datetime DEFAULT NULL,
  `status` smallint(6) DEFAULT NULL,
  `role` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 |

The users_user table has one row inside it with an id value of 1, so the user FK reference should be ok. The subreddits_subreddit table is empty (I just made it)

Any ideas on what to do? Thank you.

Lucas Ou-Yang
  • 5,505
  • 13
  • 43
  • 62

1 Answers1

4

desc is a keyword for use with order by and whatnot

Escape desc by putting backtick magic quotes:

insert into subreddits_subreddit(name, `desc`, admin_id)
  values('firstSubreddit', 'This is a test.', 1)
Bohemian
  • 412,405
  • 93
  • 575
  • 722
Jon B
  • 497
  • 1
  • 9
  • 25