I'm trying to do a final 'tournament' project in intro to relational databases course by udacity. Here's a link to the project's description:
https://docs.google.com/document/d/16IgOm4XprTaKxAa8w02y028oBECOoB1EI1ReddADEeY/pub?embedded=true
I've got a file titled tournament.sql in which database 'tournament' and two tables 'matches' and 'players' are defined:
DROP DATABASE IF EXISTS tournament;
CREATE DATABASE tournament;
CREATE TABLE IF NOT EXISTS matches (
id SERIAL PRIMARY KEY,
player1 integer references players (id),
player2 integer references players (id)
);
CREATE TABLE IF NOT EXISTS players (
id SERIAL PRIMARY KEY,
name varchar(40)
);
I also defined bodies of two functions deleteMatches and deletePlayers in tournament.py file:
import psycopg2
def connect():
"""Connect to the PostgreSQL database. Returns a database connection."""
return psycopg2.connect("dbname=tournament")
def deleteMatches():
"""Remove all the match records from the database."""
conn = connect()
c = conn.cursor()
c.execute("TRUNCATE TABLE matches;")
conn.commit()
conn.close()
def deletePlayers():
"""Remove all the player records from the database."""
conn = connect()
c = conn.cursor()
c.execute("TRUNCATE TABLE players;")
conn.commit()
conn.close()
There's one more python file predefined/prebuilt by the author of the whole course 'tournament_test.py', which can be executed to check if all required functions in tournament.py work fine/do their job. That file 'tournament_test.py' is executed in Virtual Machine from command line and in my case produces following error:
vagrant@vagrant-ubuntu-trusty-32:/vagrant/tournament$ python tournament_test.py
Traceback (most recent call last):
File "tournament_test.py", line 151, in
testCount()
File "tournament_test.py", line 17, in testCount
deleteMatches()
File "/vagrant/tournament/tournament.py", line 16, in deleteMatches
c.execute("TRUNCATE TABLE matches;")
psycopg2.ProgrammingError: relation "matches" does not exist
Does anyone know what's wrong with my code? I'm starting to lose patience. I've spent several hours trying to figure out, what's wrong and I can find any information that would be helpful. This course is so bad, sloppy and unprofessional. I just can't find right words to express my frustration.
"You probably already solved this on your own like I had to, however if you are still searching or for anyone else who may come across this thread. I am also taking this course and came across this beginner problem.
This was user error. I was connecting to vagrant and the tournament database in the wrong way.
After logging into vagrant I was in the right folder accessing the right database but in the wrong method.
Error:
Once in vagrant I went to psql as user vagrant and imported the file.
\i tournament.sql
Then I connected to the database.
\c tournament
Then I was exiting psql to run the file and getting the relation does not exist error.
I needed to do one more step.
FIX:
Once connected and logged into the database tournament. I needed to import the tournament.sql file again.
That created the relations within the actual database and not just vagrant or wherever I was creating them before.
so from Vagrant after the command Vagrant ssh # run these commands separately cd /vagrant/tournament/
psql
\i tournament.sql
\c tournament
\i tournament
#last check to verify your relations were created
\dt
\d (table or view)
That is what did it for me. The rest of the project was easy. I hope this helps anyone searching for the answer on here." My q&a
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.