每次我 run/deploy 应用程序时,Heroku postgres DB 都会丢弃更改
Heroku postgres DB discards changes every time I run/deploy the application
我正在 python 中使用 python-telegram-bot 框架开发电报机器人。
现在它作为 dyno worker 部署在 heroku 上,我还使用 hobby-dev 计划将它链接到 Heroku Postgres 数据库。
问题是,每次我部署应用程序(或在本地重新运行它,本地版本也连接到同一个数据库)数据库丢弃应用程序对它所做的所有更改,而 运行宁。
一个非常奇怪的事实是,即使我添加到 table 的行消失了,数据库看起来仍以某种方式跟踪这些更改,例如 "serial" 的值'id' 列考虑删除的行(参见下面的 table)。
这是数据库包含的 table 之一("channel and "creator" 列包含虚假的电报聊天 ID,因为它们与案例无关):
id | question | creation | mode | status | channel | creator
----+---------------+----------------------------+------+--------+----------------+-----------
1 | PollQuestion1 | 2019-06-08 13:08:25.240002 | S | t | 0001 | 002
2 | PollQuestion2 | 2019-06-08 13:08:26.830526 | S | t | 0001 | 002
3 | PollQuestion3 | 2019-06-08 23:31:21.574572 | S | t | 0001 | 002
4 | Poll4Question | 2019-06-09 16:35:58.440345 | S | t | 0001 | 002
7 | PQ5 | 2019-06-09 17:42:14.172598 | S | f | 0001 | 002
ID 为 5 和 6 的民意调查由我的应用程序添加,但在重新部署后消失了,但正如您所见,我从 heroku pg:psql[手动添加的最后一个民意调查] =46=] 终端有 id = 7.
这是一个与数据库交互的示例函数:
def execute_query(query):
try:
res = cur.execute("""{}""".format(query))
return res
except psycopg2.Error as e:
logger.error("Failed executing query {0}: ({1}) {2}".format(query, e.diag.severity, e.diag.message_primary))
def test_command():
execute_query("insert into poll (question, mode, status, channel, creator) values ('PollT1', 'S', true, '{0}', '{1}')".format(0001, 0002))
如果我随后执行查询
"select * from Poll"
在应用程序中,可以在那里找到第一个查询的结果。
相反,如果我 运行 相同
"select * from Poll"
query after re-deploying/re-running locally the app, test() 函数中包含的查询结果缺失(从上面的 table 可以看出,我执行了两次相同的操作,因此我后来手动添加的行的 id = 7)。
我知道 heroku 采用的临时文件系统,但我也读到这不应该适用于 Heroku Postgres,因此数据库应该存储我在我的应用程序中所做的更改,尽管它显然不是这样。
注意 1:我在发出 "INSERT" 查询和 "UPDATE" 查询时遇到了这个问题。
注 2:在执行这些操作时,我不断监视应用程序的控制台输出,没有发生任何错误。
有没有人遇到同样的问题并且知道如何解决,或者可以指出这个问题的原因?
您需要通过调用 connection.commit()
将更改提交到数据库。
commit()
Commit any pending transaction to the database.
By default, Psycopg opens a transaction before executing the first command: if commit()
is not called, the effect of any data manipulation will be lost.
假设您有一个 conn
变量代表您的连接对象,您可以在执行查询后立即调用 conn.commit()
。然而,确保这一点的更安全、更 Pythonic 的方法是对连接和游标使用 with
-statement 上下文管理器。
def execute_query(query):
try:
with conn:
with conn.cursor() as cur:
res = cur.execute("""{}""".format(query))
return res
except psycopg2.Error as e:
logger.error("Failed executing query {0}: ({1}) {2}".format(query, e.diag.severity, e.diag.message_primary))
如果查询成功,这将自动调用 commit()
,如果查询不成功,则自动调用 rollback()
。
我正在 python 中使用 python-telegram-bot 框架开发电报机器人。
现在它作为 dyno worker 部署在 heroku 上,我还使用 hobby-dev 计划将它链接到 Heroku Postgres 数据库。
问题是,每次我部署应用程序(或在本地重新运行它,本地版本也连接到同一个数据库)数据库丢弃应用程序对它所做的所有更改,而 运行宁。
一个非常奇怪的事实是,即使我添加到 table 的行消失了,数据库看起来仍以某种方式跟踪这些更改,例如 "serial" 的值'id' 列考虑删除的行(参见下面的 table)。
这是数据库包含的 table 之一("channel and "creator" 列包含虚假的电报聊天 ID,因为它们与案例无关):
id | question | creation | mode | status | channel | creator
----+---------------+----------------------------+------+--------+----------------+-----------
1 | PollQuestion1 | 2019-06-08 13:08:25.240002 | S | t | 0001 | 002
2 | PollQuestion2 | 2019-06-08 13:08:26.830526 | S | t | 0001 | 002
3 | PollQuestion3 | 2019-06-08 23:31:21.574572 | S | t | 0001 | 002
4 | Poll4Question | 2019-06-09 16:35:58.440345 | S | t | 0001 | 002
7 | PQ5 | 2019-06-09 17:42:14.172598 | S | f | 0001 | 002
ID 为 5 和 6 的民意调查由我的应用程序添加,但在重新部署后消失了,但正如您所见,我从 heroku pg:psql[手动添加的最后一个民意调查] =46=] 终端有 id = 7.
这是一个与数据库交互的示例函数:
def execute_query(query):
try:
res = cur.execute("""{}""".format(query))
return res
except psycopg2.Error as e:
logger.error("Failed executing query {0}: ({1}) {2}".format(query, e.diag.severity, e.diag.message_primary))
def test_command():
execute_query("insert into poll (question, mode, status, channel, creator) values ('PollT1', 'S', true, '{0}', '{1}')".format(0001, 0002))
如果我随后执行查询
"select * from Poll"
在应用程序中,可以在那里找到第一个查询的结果。
相反,如果我 运行 相同
"select * from Poll"
query after re-deploying/re-running locally the app, test() 函数中包含的查询结果缺失(从上面的 table 可以看出,我执行了两次相同的操作,因此我后来手动添加的行的 id = 7)。
我知道 heroku 采用的临时文件系统,但我也读到这不应该适用于 Heroku Postgres,因此数据库应该存储我在我的应用程序中所做的更改,尽管它显然不是这样。
注意 1:我在发出 "INSERT" 查询和 "UPDATE" 查询时遇到了这个问题。 注 2:在执行这些操作时,我不断监视应用程序的控制台输出,没有发生任何错误。
有没有人遇到同样的问题并且知道如何解决,或者可以指出这个问题的原因?
您需要通过调用 connection.commit()
将更改提交到数据库。
commit()
Commit any pending transaction to the database.
By default, Psycopg opens a transaction before executing the first command: if
commit()
is not called, the effect of any data manipulation will be lost.
假设您有一个 conn
变量代表您的连接对象,您可以在执行查询后立即调用 conn.commit()
。然而,确保这一点的更安全、更 Pythonic 的方法是对连接和游标使用 with
-statement 上下文管理器。
def execute_query(query):
try:
with conn:
with conn.cursor() as cur:
res = cur.execute("""{}""".format(query))
return res
except psycopg2.Error as e:
logger.error("Failed executing query {0}: ({1}) {2}".format(query, e.diag.severity, e.diag.message_primary))
如果查询成功,这将自动调用 commit()
,如果查询不成功,则自动调用 rollback()
。