Celestial Bodies Database - Build a Celestial Bodies Database

Tell us what’s happening:
Describe your issue in detail here.

cannot pass the following

  • 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

Your code so far
please tell me what am I doing wrong here?
why the above checks are not getting passsed?
upon describing my tables in the universe DB
with \d

universe=> \d star
                                              Table "public.star"
+---------------------+------------------------+-----------+----------+---------------------------------------+
|       Column        |          Type          | Collation | Nullable |                Default                |
+---------------------+------------------------+-----------+----------+---------------------------------------+
| star_id             | integer                |           | not null | nextval('star_star_id_seq'::regclass) |
| name                | character varying(100) |           | not null |                                       |
| spectral_type       | character varying(50)  |           |          |                                       |
| luminosity          | numeric(5,2)           |           |          |                                       |
| mass                | numeric(5,2)           |           |          |                                       |
| distance_from_earth | numeric(5,2)           |           |          |                                       |
| is_active           | boolean                |           |          |                                       |
| temperature         | numeric(5,2)           |           |          |                                       |
| galaxy_id           | integer                |           |          |                                       |
+---------------------+------------------------+-----------+----------+---------------------------------------+
Indexes:
    "star_pkey" PRIMARY KEY, btree (star_id)
    "star_name_key" UNIQUE CONSTRAINT, btree (name)
Foreign-key constraints:
    "star_galaxy_id_fkey" FOREIGN KEY (galaxy_id) REFERENCES galaxy(galaxy_id)
Referenced by:
    TABLE "planet" CONSTRAINT "planet_star_id_fkey" FOREIGN KEY (star_id) REFERENCES star(star_id)

universe=> \d planet
                                              Table "public.planet"
+--------------------+-----------------------+-----------+----------+-------------------------------------------+
|       Column       |         Type          | Collation | Nullable |                  Default                  |
+--------------------+-----------------------+-----------+----------+-------------------------------------------+
| planet_id          | integer               |           | not null | nextval('planet_planet_id_seq'::regclass) |
| name               | character varying(50) |           | not null |                                           |
| type               | character varying(50) |           |          |                                           |
| size               | numeric(5,2)          |           |          |                                           |
| mass               | numeric(5,2)          |           |          |                                           |
| distance_from_star | numeric(5,2)          |           |          |                                           |
| orbital_period     | numeric(5,2)          |           |          |                                           |
| is_habitable       | boolean               |           |          |                                           |
| star_id            | integer               |           |          |                                           |
| planet_position    | integer               |           | not null |                                           |
| random_number      | integer               |           |          |                                           |
+--------------------+-----------------------+-----------+----------+-------------------------------------------+
Indexes:
    "planet_pkey" PRIMARY KEY, btree (planet_id)
    "planet_name_key" UNIQUE CONSTRAINT, btree (name)
Foreign-key constraints:
    "planet_star_id_fkey" FOREIGN KEY (star_id) REFERENCES star(star_id)
Referenced by:
    TABLE "moon" CONSTRAINT "moon_planet_id_fkey" FOREIGN KEY (planet_id) REFERENCES planet(planet_id)

universe=> \d moon
                                               Table "public.moon"
+----------------------+-----------------------+-----------+----------+-----------------------------------------+
|        Column        |         Type          | Collation | Nullable |                 Default                 |
+----------------------+-----------------------+-----------+----------+-----------------------------------------+
| moon_id              | integer               |           | not null | nextval('moon_moon_id_seq'::regclass)   |
| name                 | character varying(50) |           | not null |                                         |
| size                 | numeric(5,2)          |           |          |                                         |
| mass                 | numeric(5,2)          |           |          |                                         |
| distance_from_planet | numeric(5,2)          |           |          |                                         |
| orbital_period       | numeric(5,2)          |           |          |                                         |
| is_satellite         | boolean               |           |          |                                         |
| planet_id            | integer               |           | not null | nextval('moon_planet_id_seq'::regclass) |
+----------------------+-----------------------+-----------+----------+-----------------------------------------+
Indexes:
    "moon_pkey" PRIMARY KEY, btree (moon_id)
    "moon_name_key" UNIQUE CONSTRAINT, btree (name)
Foreign-key constraints:
    "moon_planet_id_fkey" FOREIGN KEY (planet_id) REFERENCES planet(planet_id)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36

Challenge: Celestial Bodies Database - Build a Celestial Bodies Database

Link to the challenge:

is it related to Insert query.
as the first check

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

got resolved, after inserting into the table.

I think you added something extra to the star table:


You should remove what is marked in red.
Try something like this:
image
Only “planet” must have a foreign key in “star”, not “galaxy”.
Do you have a screenshot of the “galaxy” table?

Ok I will look into this,
thanks,
here is the Galaxy table

universe=> \d galaxy
                                              Table "public.galaxy"
+---------------------+-----------------------+-----------+----------+-------------------------------------------+
|       Column        |         Type          | Collation | Nullable |                  Default                  |
+---------------------+-----------------------+-----------+----------+-------------------------------------------+
| galaxy_id           | integer               |           | not null | nextval('galaxy_galaxy_id_seq'::regclass) |
| name                | character varying(50) |           | not null |                                           |
| description         | text                  |           |          |                                           |
| size                | numeric(5,2)          |           |          |                                           |
| distance_from_earth | numeric(5,2)          |           |          |                                           |
| is_active           | boolean               |           |          |                                           |
+---------------------+-----------------------+-----------+----------+-------------------------------------------+
Indexes:
    "galaxy_pkey" PRIMARY KEY, btree (galaxy_id)
    "galaxy_name_unique" UNIQUE CONSTRAINT, btree (name)
Referenced by:
    TABLE "celestial_body" CONSTRAINT "celestial_body_galaxy_id_fkey" FOREIGN KEY (galaxy_id) REFERENCES galaxy(galaxy_id)
    TABLE "star" CONSTRAINT "star_galaxy_id_fkey" FOREIGN KEY (galaxy_id) REFERENCES galaxy(galaxy_id)

also when i started to do the insert queriess,
those checks were getting passed.

“galaxy” has only one PRIMARY KEY and UNIQUE CONSTRAINT. It has no FOREIGN KEY :
image
My UNIQUE is on my “constellation” column that I added to the Galaxy Table. I see your UNIQUE is on the “name” column in your galaxy table. Is not a problem. Only no FOREIGN KEY on " galaxy ".

yes I have added foreign key likewise for every table

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.