psycopg2.ProgrammingError:关系“匹配”;不存在 [英] psycopg2.ProgrammingError: relation "matches" does not exist

查看:838
本文介绍了psycopg2.ProgrammingError:关系“匹配”;不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个最后的比赛项目,以大胆地介绍关系数据库课程。以下是项目说明的链接:

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

我已经得到了一个名为Tournament.sql的文件,其中定义了数据库锦标赛以及两个表比赛和玩家:

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

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

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

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

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.

错误:

在无业游民中,我以无业游民身份进入psql并导入了文件。

Once in vagrant I went to psql as user vagrant and imported the file.

\i tournament.sql

然后我连接到数据库。

\c tournament

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

Then I was exiting psql to run the file and getting the relation does not exist error.

我需要再做一步。

FIX:

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

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.

因此在命令Vagrant ssh
之后是从Vagrant获得的#b $ b cd / vagrant / tournament /

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

这篇关于psycopg2.ProgrammingError:关系“匹配”;不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆