繁体   English   中英

psycopg2.ProgrammingError:关系“匹配”不存在

[英]psycopg2.ProgrammingError: relation “matches” does not exist

我正在尝试通过udacity向关系数据库课程做最后的“比赛”项目介绍。 这是项目描述的链接:

https://docs.google.com/document/d/16IgOm4XprTaKxAa8w02y028oBECOoB1EI1ReddADEeY/pub?embedded=true

我有一个名为Tournament.sql的文件,其中定义了数据库“锦标赛”和两个表“比赛”和“玩家”:

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)
);

我还在Tournament.py文件中定义了两个函数deleteMatches和deletePlayers的主体:

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()

整个课程“ tournament_test.py”的作者还预定义/预先构建了一个python文件,可以执行该文件来检查Tournament.py中所有必需的功能是否都能正常工作。 该文件“ tournament_test.py”是在虚拟机中从命令行执行的,在我的情况下会产生以下错误:

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

有人知道我的代码有什么问题吗? 我开始失去耐心了。 我花了几个小时试图找出问题所在,并且可以找到任何有帮助的信息。 这门课是如此糟糕,草率且不专业。 我只是找不到合适的词来表达我的沮丧。

“您可能已经像我必须的那样自行解决了这个问题,但是,如果您仍在搜索或寻找其他可能遇到此问题的人。我也正在学习此课程,并且遇到了这个初学者的问题。

这是用户错误。 我以错误的方式连接到无业游民和锦标赛数据库。

登录到无业游民后,我在正确的文件夹中访问了正确的数据库,但是使用了错误的方法。

错误:

一旦流浪汉,我就以用户流浪汉的身份去了psql并导入了文件。

\i tournament.sql

然后我连接到数据库。

\c tournament

然后我退出了psql以运行该文件,并且获取该关系不存在错误。

我需要再做一步。

固定:

一旦连接并登录数据库锦标赛。 我需要再次导入Tournament.sql文件。

那在实际数据库中创建了关系,而不仅仅是无聊的或者我以前创建关系的地方。

因此在Vagrant ssh命令之后从Vagrant进行#分别运行以下命令cd / vagrant / tournament /

psql

\i tournament.sql

\c tournament

\i tournament

#last check to verify your relations were created
\dt
\d (table or view)

那就是为我做的。 该项目的其余部分很容易。 我希望这可以帮助任何在这里搜索答案的人。” 的问答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM