I have 4 tables(galaxy, moon, planet, star) each have distinct id columns that are primary keys and galaxy_id references moon_id , moon_id references planet_id and planet_id_references star_id. But each require a different minimum amount of rows. My error is that I am violating foreign key constraint. If I add 12 rows to planet, it violates because I only have 6 entries in star. I can’t make primary key not NULL, how to solve this issue?
Your code so far
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Celestial Bodies Database - Build a Celestial Bodies Database
I don’t understand your issue, when I added the rows to planet most had the same star_id as they where solar system planets. Did you add additional constraints to the foreign key columns?
Yes I also just added the milky way planets, I think my issue is that I did not implement the auto increment. So now when I add rows to a table and another table has less rows, that table primary key doesn’t automatically increment, thereby violating the constraint. I have tried adding the auto_increment to an existing table but I recieve syntax errors
universe=> ALTER TABLE galaxy ADD galaxy_id INT PRIMARY KEY AUTO_INCREMENT FIRST;
ERROR: syntax error at or near “AUTO_INCREMENT”
LINE 1: ALTER TABLE galaxy ADD galaxy_id INT PRIMARY KEY AUTO_INCREM…
I think I should drop my foreign key constraint or just start again from scratch. Because Primary Key not null is the constraint that I am violating when the tables have varying amoount of entries.
Here are snapshots of the work in terminal. Planet table requires 6 entries, as soon as I attempt to add a 7th, I get a constraint violation references that star_id(PK) will result in a NULL entry. I think the auto increment will make it auto increment if the planet table that has foreign key(planet_id) associated with has more than 6 entries.
Man I just started from scratch because my terminal keep bugging out. So I am trying to make a dump file on my new project but it is not working. Should I delete the old saved universe.sql dump file on gitpodio. My terminal universe=> prompt is looking like this universe-> after attempting the pg_dump -cC --inserts -U freecodecamp universe > universe.sql in postgretsql because there is no bash.
But I satisfied everything but they aren’t being checked, even the obvious ones. I think there might be a communication issue because I restarted the task, and the unfinished version is being parsed to the check list. How and where do I delete unfinished version if that is the way forward.
I cant login to my universe.sql. “psql -U postgres < universe.sql” This doesn’t generate anything. After I type “\c universe” It says universe.sql does not exist.
This is the snapshot in my gitpod workspace. I deleted the older version in hopes I could get all the code to pass. Is there anyway I can still access my work?
Hey @Teller I hope you are well. Sorry. I downloaded from the workspace onto pc and saved it on a github repository. I tried to push the boilerplate from the Terminal but it says Ray150789 doesnt exist.
Sooo you are saying my current workspace is faulty? The workspaces are created after you attempt the project, how do you create a new one. Does the faulty workspace have any influence on my current issue. Im attempting to create a table “CREATE TABLE students()” And its not working.
Is this what u mean by recreating form a dump. How do I diagnose this? When I attempt to connect with universe database, I get “postgres-> \c universe
connection to server at “127.0.0.1”, port 5432 failed: FATAL: database “universe” does not exist
Previous connection kept”.
using psql --username=freecodecamp --dbname=postgres you are connecting to the postgres database, I guess you need to connect to the universe database. At least, did you create the universe database before starting?
also -U postgres < universe.sql is a bash command, not a postgres one, you need to do that in the terminal not connected to the database