PSQL CELESTIAL BODIES DATABASE

Hii,

I have a question. I have to make a foreign key to connect rows and tables with eachother.

  • Each “star” should have a foreign key that references one of the rows in galaxy

  • Each “planet” should have a foreign key that references one of the rows in star

  • Each “moon” should have a foreign key that references one of the rows in planet

But everytime I make a foreign key, It does not complete the challenge. Even after linking them all… You can check out my code here:

I used the following commands to set the foreign keys.

ALTER TABLE public.star
ADD COLUMN galaxy_id integer;

ALTER TABLE public.star
ADD CONSTRAINT star_galaxy_fkey
FOREIGN KEY (galaxy_id) REFERENCES public.galaxy(galaxy_id)
ON DELETE SET NULL;

-- Step 2: Add star_id to planet
ALTER TABLE public.planet
ADD COLUMN star_id integer;

ALTER TABLE public.planet
ADD CONSTRAINT planet_star_fkey
FOREIGN KEY (star_id) REFERENCES public.star(star_id)
ON DELETE SET NULL;

-- Step 3: Add planet_id to moon
ALTER TABLE public.moon
ADD COLUMN planet_id integer;

ALTER TABLE public.moon
ADD CONSTRAINT moon_planet_fkey
FOREIGN KEY (planet_id) REFERENCES public.planet(planet_id)
ON DELETE SET NULL;

Can someone please tell me what I do wrong? I really want to understand this.

Thanks in advance!

Okay for everybody that is gonna be stuck here also. I accidentally made the foreign keys of the data type INT instead of SERIAL. Because of this the star(galaxy_id) had no values in the rows. If you make it SERIAL (just like your primary key), It will recognize each other and you will pass the tests. If someone needs a better explanation, you can still reply on this, I’ll answer.