Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

SQL - Keys and Auto-Incrementing Values, challenge task 3

I can't seem to get this one... I'm asked to: Alter the "t_movies" table to add a foreign key called "fk_genre_id" and constrain it to reference the "t_genres" "pk_id".

Here's my attempt:

ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER NULL ADD CONTRAINT FOREIGN KEY (fk_genre_id) REFERENCES t_genres(pk_id);

Here's what I get back: Bummer! You're missing the 'fk_genre_id' column.

What am I doing wrong? Didn't I add the column with the "ADD COLUMN fk_genre_id INTEGER NULL" key words? I'm still doing something wrong, but I can't figure it out. The SQL syntax reference on the mySQL site isn't helping.

Help!

Thanks! Brandi

8 Answers

Alex Sollman
Alex Sollman
5,353 Points

Hello,

I was just having the EXACT problem you were, and found out that we were both missing a comma.

ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER NULL, ADD CONSTRAINT FOREIGN KEY (fk_genre_id) REFERENCES t_genres(pk_id);

I passed with this, hope it helps!

John Locke
John Locke
15,479 Points

I got stuck on the syntax, too. Thanks Alex!

Joshua Perry
Joshua Perry
2,962 Points

Same here. Thanks Alex!

Mohammed Hossen
Mohammed Hossen
5,591 Points

ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER, ADD CONSTRAINT FOREIGN KEY (fk_genre_id) REFERENCES t_genres(pk_id);

The code will work if you separate the two words "INTEGER, ADD" with comma. The missing comma is keep throwing an error saying 'fk_genre_id' column is missing.

Did you mean CONSTRAINT instead of CONTRAINT?

Robert Hustwick
Robert Hustwick
13,058 Points

Man! It took forever to figure out that I was typing it in wrong! Thanks.

shahardekel
shahardekel
20,306 Points

I just had the exact same problem (even used the same SQL as Brandi Bizek did. Adding the comma did the job. thanks!

If anyone still trying to find a solution add the following code.

ALTER TABLE t_movies ADD fk_genre_id INTEGER, ADD CONSTRAINT FOREIGN KEY(fk_genre_id) REFERENCES t_genres(pk_id)

Sean Flanagan
Sean Flanagan
33,235 Points

Thank you for this thread. It's just proven to be a big help in cracking this challenge! :-)

Oops! Yes, that's what I meant. I've tried it so many times... this, unfortunately, wasn't the problem. I've fixed the spelling typo and still get the same "bummer" response: ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER ADD CONSTRAINT FOREIGN KEY (fk_genre_id) REFERENCES t_genres(pk_id);

Ideas?

Hei! I found what was missing from your code... ALTER TABLE t_movies ADD COLUMN fk_genre_id INTEGER ADD CONSTRAINT FOREIGN KEY (fk_genre_id) REFERENCES t_genres(pk_id); That is your code.. and you are missing a comma between "INTEGER" and the "ADD CONSTRAINT" :D