【问题标题】:psycopg2.ProgrammingError: relation "matches" does not existpsycopg2.ProgrammingError:关系“匹配”不存在
【发布时间】:2017-06-26 16:18:46
【问题描述】:

我正在尝试在 udacity 的关系数据库入门课程中做一个最终的“锦标赛”项目。这是项目描述的链接:

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

我有一个名为tournament.sql 的文件,其中定义了数据库'tournament' 和两个表'matches' 和'players':

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

我还在锦标赛.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

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

【问题讨论】:

  • 您是否将该 SQL 文件加载到数据库中?怎么样?
  • 我不明白这个问题。将 SQL 文件加载到数据库中是什么意思?你说的是哪个数据库?我应该如何将 sql 文件加载到数据库中?你的意思是:“从锦标赛导入*”这是预定义的python文件锦标赛测试.py的顶部吗?
  • 那么您希望文件自己做什么?编写一个 SQL 命令文件非常好,但除非你真的将它们提供给数据库,否则你还不如编写童谣。您的错误是告诉您您尚未创建表,如果您从未实际运行过该文件,这是正确的。
  • 如果你的意思是将锦标赛.sql 加载到 psql 中:vagrant@vagrant-ubuntu-trusty-32:/vagrant/tournament$ psql

标签: python sql postgresql virtual-machine git-bash


【解决方案1】:

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

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

登录 vagrant 后,我​​在正确的文件夹中访问了正确的数据库,但使用了错误的方法。

错误:

在 vagrant 中,我以 vagrant 用户身份访问 psql 并导入了文件。

\i tournament.sql

然后我连接到数据库。

\c tournament

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

我需要再做一步。

修复:

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

这在实际数据库中创建了关系,而不仅仅是 vagrant 或我之前创建它们的任何地方。

所以在命令 Vagrant ssh 之后来自 Vagrant # 分别运行这些命令 cd /vagrant/锦标赛/

psql

\i tournament.sql

\c tournament

\i tournament

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

这就是为我做的。项目的其余部分很容易。我希望这可以帮助任何在这里寻找答案的人。”My q&a

【讨论】:

    猜你喜欢
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多