Python backed SQL Database returning a psycopg2.ProgrammingError: relation does not exist error when attempting to delete the data within a table?
Python backed SQL Database returning a psycopg2.ProgrammingError: relation does not exist error when attempting to delete the data within a table?
简介:我一直致力于为我的关系数据库使用 Vagrant 运行 一个 Ubuntu VM 构建一个 Python 支持的 PostgreSQL 瑞士风格锦标赛数据库优达学城的课程。
问:为什么我删除已有table中的信息时提示table不存在?
我进行了一般搜索以进行研究
psycopg2.ProgrammingError
,但这些信息大多是针对其他情况的,我无法建立逻辑联系来解决这个问题。我还发现了另一个 Stack Overflow 线程,该线程接近于同一问题 other thread here,但是该问题也从未得到解决。所以我提出了这个问题,试图找到能够理解我的难题并提供某种提示或线索让我朝着正确的方向努力的人。
我已将锦标赛数据库 SQL 文件导入 Vagrant VM。这删除了两个 tables(球员和比赛)和数据库(锦标赛),然后重新创建了这个数据库和这些 tables。所以我知道 table (匹配项)存在。然而,这就是我得到的...
Vagrant:(当前 vagrant 错误)
vagrant@vagrant:/vagrant/tournament$ python tournament_test.py
Traceback (most recent call last):
File "tournament_test.py", line 151, in <module>
testCount()
File "tournament_test.py", line 17, in testCount
deleteMatches()
File "/vagrant/tournament/tournament.py", line 18, in deleteMatches
cursor.execute("DELETE FROM matches;")
psycopg2.ProgrammingError: relation "matches" does not exist
LINE 1: DELETE FROM matches;
^
SQL:(全部来自tournament.sql)
-- Table definitions for the tournament project.
--
-- Put your SQL 'create table' statements in this file; also 'create view'
-- statements if you choose to use it.
--
-- You can write comments in this file by starting them with two dashes,
like
-- these lines here.
DROP TABLE matches;
DROP TABLE players;
DROP DATABASE tournament;
CREATE DATABASE tournament;
CREATE TABLE players (player_id SERIAL UNIQUE PRIMARY KEY, name
VARCHAR(40));
CREATE TABLE matches (match_id SERIAL UNIQUE PRIMARY KEY,
winner INTEGER REFERENCES players(player_id),
loser INTEGER REFERENCES players (player_id));
Python:(来自 tournament.py 的最小值)
#!/usr/bin/env python
#
# tournament.py -- implementation of a Swiss-system tournament
#
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."""
db = connect()
cursor = db.cursor()
cursor.execute("TRUNCATE matches CASCADE;")
db.commit()
db.close()
Python:(来自 tournament_test.py 的最小值)这是验证 tournament.py 函数正在使用 tournament.sql 数据库的文件。
#!/usr/bin/env python
#
# Test cases for tournament.py
from tournament import *
def testCount():
"""
Test for initial player count,
player count after 1 and 2 players registered,
player count after players deleted.
"""
deleteMatches()
deletePlayers()
c = countPlayers()
if c == '0':
raise TypeError(
"countPlayers should return numeric zero, not string '0'.")
if c != 0:
raise ValueError("After deletion, countPlayers should return zero.")
print "1. countPlayers() returns 0 after initial deletePlayers()
execution."
registerPlayer("Chandra Nalaar")
c = countPlayers()
if c != 1:
raise ValueError(
"After one player registers, countPlayers() should be 1. Got
{c}".format(c=c))
print "2. countPlayers() returns 1 after one player is registered."
registerPlayer("Jace Beleren")
c = countPlayers()
if c != 2:
raise ValueError(
"After two players register, countPlayers() should be 2. Got
{c}".format(c=c))
print "3. countPlayers() returns 2 after two players are registered."
deletePlayers()
c = countPlayers()
if c != 0:
raise ValueError(
"After deletion, countPlayers should return zero.")
print "4. countPlayers() returns zero after registered players are
deleted.\n5. Player records successfully deleted."
testCount()
Vagrant(确认 table 'matches' 存在于数据库中):
vagrant=> \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+---------
public | matches | table | vagrant
public | players | table | vagrant
(2 rows)
更新 2:对评论的澄清
我在 python 文件的开头连接到数据库。
def connect()
"""Connect to the PostgreSQL database. Returns a database
connection."""
return psycopg2.connect("dbname=tournament")
这是用户错误。我以错误的方式连接到 vagrant 和锦标赛数据库。
登录 vagrant 后,我在正确的文件夹中访问了正确的数据库,但方法错误。
错误:
进入 vagrant 后,我以 vagrant 用户身份访问 psql 并导入文件。
\i tournament.sql
然后我连接到数据库。
\c tournament
然后我退出 psql 到 运行 文件并得到关系不存在错误。
我需要再做一步。
修复:
连接并登录数据库锦标赛后。我需要再次导入 tournament.sql 文件。
这在实际数据库中创建了关系,而不仅仅是 vagrant 或我之前创建它们的任何地方。
so 从 Vagrant 命令 Vagrant ssh 之后
# 运行 这些命令分开
cd /vagrant/tournament/
psql
\i tournament.sql
\c tournament
\i tournament
#last check to verify your relations were created
\dt
\d (table or view)
这就是为我做的。项目的其余部分很容易。我希望这可以帮助任何人在这里寻找答案,并且只找到更多未回答但受到严厉批评的问题。感谢所有看到我的初学者问题并给我负面评价或停止批评我的问题格式的专家。我仍在学习,如果你们中的任何人真的帮助我理解或解决了这个问题,我将无法在 3 天的时间里独自完成所有工作。
简介:我一直致力于为我的关系数据库使用 Vagrant 运行 一个 Ubuntu VM 构建一个 Python 支持的 PostgreSQL 瑞士风格锦标赛数据库优达学城的课程。
问:为什么我删除已有table中的信息时提示table不存在?
我进行了一般搜索以进行研究
psycopg2.ProgrammingError
,但这些信息大多是针对其他情况的,我无法建立逻辑联系来解决这个问题。我还发现了另一个 Stack Overflow 线程,该线程接近于同一问题 other thread here,但是该问题也从未得到解决。所以我提出了这个问题,试图找到能够理解我的难题并提供某种提示或线索让我朝着正确的方向努力的人。
我已将锦标赛数据库 SQL 文件导入 Vagrant VM。这删除了两个 tables(球员和比赛)和数据库(锦标赛),然后重新创建了这个数据库和这些 tables。所以我知道 table (匹配项)存在。然而,这就是我得到的...
Vagrant:(当前 vagrant 错误)
vagrant@vagrant:/vagrant/tournament$ python tournament_test.py
Traceback (most recent call last):
File "tournament_test.py", line 151, in <module>
testCount()
File "tournament_test.py", line 17, in testCount
deleteMatches()
File "/vagrant/tournament/tournament.py", line 18, in deleteMatches
cursor.execute("DELETE FROM matches;")
psycopg2.ProgrammingError: relation "matches" does not exist
LINE 1: DELETE FROM matches;
^
SQL:(全部来自tournament.sql)
-- Table definitions for the tournament project.
--
-- Put your SQL 'create table' statements in this file; also 'create view'
-- statements if you choose to use it.
--
-- You can write comments in this file by starting them with two dashes,
like
-- these lines here.
DROP TABLE matches;
DROP TABLE players;
DROP DATABASE tournament;
CREATE DATABASE tournament;
CREATE TABLE players (player_id SERIAL UNIQUE PRIMARY KEY, name
VARCHAR(40));
CREATE TABLE matches (match_id SERIAL UNIQUE PRIMARY KEY,
winner INTEGER REFERENCES players(player_id),
loser INTEGER REFERENCES players (player_id));
Python:(来自 tournament.py 的最小值)
#!/usr/bin/env python
#
# tournament.py -- implementation of a Swiss-system tournament
#
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."""
db = connect()
cursor = db.cursor()
cursor.execute("TRUNCATE matches CASCADE;")
db.commit()
db.close()
Python:(来自 tournament_test.py 的最小值)这是验证 tournament.py 函数正在使用 tournament.sql 数据库的文件。
#!/usr/bin/env python
#
# Test cases for tournament.py
from tournament import *
def testCount():
"""
Test for initial player count,
player count after 1 and 2 players registered,
player count after players deleted.
"""
deleteMatches()
deletePlayers()
c = countPlayers()
if c == '0':
raise TypeError(
"countPlayers should return numeric zero, not string '0'.")
if c != 0:
raise ValueError("After deletion, countPlayers should return zero.")
print "1. countPlayers() returns 0 after initial deletePlayers()
execution."
registerPlayer("Chandra Nalaar")
c = countPlayers()
if c != 1:
raise ValueError(
"After one player registers, countPlayers() should be 1. Got
{c}".format(c=c))
print "2. countPlayers() returns 1 after one player is registered."
registerPlayer("Jace Beleren")
c = countPlayers()
if c != 2:
raise ValueError(
"After two players register, countPlayers() should be 2. Got
{c}".format(c=c))
print "3. countPlayers() returns 2 after two players are registered."
deletePlayers()
c = countPlayers()
if c != 0:
raise ValueError(
"After deletion, countPlayers should return zero.")
print "4. countPlayers() returns zero after registered players are
deleted.\n5. Player records successfully deleted."
testCount()
Vagrant(确认 table 'matches' 存在于数据库中):
vagrant=> \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+---------
public | matches | table | vagrant
public | players | table | vagrant
(2 rows)
更新 2:对评论的澄清
我在 python 文件的开头连接到数据库。
def connect()
"""Connect to the PostgreSQL database. Returns a database
connection."""
return psycopg2.connect("dbname=tournament")
这是用户错误。我以错误的方式连接到 vagrant 和锦标赛数据库。
登录 vagrant 后,我在正确的文件夹中访问了正确的数据库,但方法错误。
错误:
进入 vagrant 后,我以 vagrant 用户身份访问 psql 并导入文件。
\i tournament.sql
然后我连接到数据库。
\c tournament
然后我退出 psql 到 运行 文件并得到关系不存在错误。
我需要再做一步。
修复:
连接并登录数据库锦标赛后。我需要再次导入 tournament.sql 文件。
这在实际数据库中创建了关系,而不仅仅是 vagrant 或我之前创建它们的任何地方。
so 从 Vagrant 命令 Vagrant ssh 之后 # 运行 这些命令分开 cd /vagrant/tournament/
psql
\i tournament.sql
\c tournament
\i tournament
#last check to verify your relations were created
\dt
\d (table or view)
这就是为我做的。项目的其余部分很容易。我希望这可以帮助任何人在这里寻找答案,并且只找到更多未回答但受到严厉批评的问题。感谢所有看到我的初学者问题并给我负面评价或停止批评我的问题格式的专家。我仍在学习,如果你们中的任何人真的帮助我理解或解决了这个问题,我将无法在 3 天的时间里独自完成所有工作。