# Celestial Bodies Database Project

**URL:** <https://forum.freecodecamp.org/t/celestial-bodies-database-project/590888>\
**Category:** General\
**Created:** [February 8, 2023, 7:12pm UTC](https://forum.freecodecamp.org/t/celestial-bodies-database-project/590888 "2023-02-08T19:12:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kpqkhanh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kpqkhanh/32/304500_2.png) [@kpqkhanh](https://forum.freecodecamp.org/u/kpqkhanh)\
**Post date:** [February 8, 2023, 7:12pm UTC](https://forum.freecodecamp.org/t/celestial-bodies-database-project/590888/1 "2023-02-08T19:12:36Z")

</div>

Hi,  
Although I have tried multiple times trying to add a foreign key to the table(star) with the command:  
ALTER TABLE star ADD FOREIGN KEY (galaxy\_id) REFERENCES galaxy(galaxy\_id);  
The test does not anyhow return a passed result.  
Here is what the system displayed when I press ‘run’:  
SUBTASKS 1.1 :10 Each “star” row should have a foreign key that references one of the “galaxy” rows  
What is the solution to this? Ty

---

<div class="post-metadata">

**Author:** ![Nathan.clark88](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nathan.clark88/32/300368_2.png) [@Nathan.clark88](https://forum.freecodecamp.org/u/Nathan.clark88)\
**Post date:** [February 8, 2023, 8:37pm UTC](https://forum.freecodecamp.org/t/celestial-bodies-database-project/590888/2 "2023-02-08T20:37:47Z")

</div>

The error you’re encountering is likely due to one or more of the existing “star” rows having a `galaxy_id` value that doesn’t match any of the existing `galaxy_id` values in the “galaxy” table.

Before adding a foreign key constraint to the “star” table, you need to ensure that there are no existing rows in the “star” table with invalid `galaxy_id` values. You can do this by either:

1. Deleting the “star” rows with invalid `galaxy_id` values, or
2. Updating the “star” rows with invalid `galaxy_id` values to have a valid `galaxy_id` that matches one of the values in the “galaxy” table.

Once you have fixed the data, you should be able to add the foreign key constraint to the “star” table using the ALTER TABLE statement you posted in your question.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 10, 2023, 8:38am UTC](https://forum.freecodecamp.org/t/celestial-bodies-database-project/590888/3 "2023-08-10T08:38:40Z")

</div>

Closed on August 10, 2023, 8:38am UTC

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