gpt4 book ai didi

ruby-on-rails - 缺少 Postgres 列

转载 作者:搜寻专家 更新时间:2023-10-30 23:25:58 26 4
gpt4 key购买 nike

Rails 应用程序制作的新 postgres 数据库出现最奇怪的错误。

我创建了一个带有引用字段 (game_id) 的新表 (images)。

    create_table :images do |t|
t.string :title
t.references :game
t.string :file
t.integer :image_type, default: 0

t.timestamps
end

我在表格中插入了 6000 多张图片,并引用了游戏 table 。工作几个月后,该表现在丢失了 game_id 列。它完全消失了。没有运行任何迁移,开发数据库工作正常,它仍然有那个列。自启动该应用程序以来,我检查了日志(它包含所有迁移),在任何地方都没有删除列调用的痕迹。 postgres 历史上也没有任何内容。日志显示正确的插入

INSERT INTO "images" ("game_id", "file", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"ESC[0m  [["game_id", 4], ["file", "r6hhvtaozowkcf06bswz.jpg"], ["created_at", "2019-03-05 14:40:33.612203"], ["updated_at", "2019-03-05 14:40:33.612203"]]

现在我只有错误

column images.game_id does not exist

我很沮丧,因为我丢失了所有数据,有人知道该列被删除的原因吗?是否有可能取回数据,还是我应该从头开始?我也没有升级服务器,连

最佳答案

列不会自己消失,一定是某人或某物丢弃了它。

这个建议可能来不及了,但是如果你在 postgresql.conf 中将 log_statement 设置为 'ddl',你会得到所有的记录了 ALTER TABLE 语句,这将帮助您找到导致问题的人或原因。

如果您愿意弄乱目录表,您可以恢复该列,除非该表已经被 VACUUM (FULL) 或类似的重写。

我建议您不要在生产数据库上执行此操作,而应在使用 pg_basebackup 生成的副本上执行此操作。然后您可以 pg_dump 修复后的表并替换生产数据库中损坏的表。

假设您的表名为 tab 并且您删除了一个 integerb

首先,让我们验证这是否真的发生了:

SELECT * FROM pg_attribute
WHERE attrelid = 'tab'::regclass
AND attnum > 0
ORDER BY attnum;

attrelid | attname | atttypid | attstattarget | attlen | attnum | attndims | attcacheoff | atttypmod | attbyval | attstorage | attalign | attnotnull | atthasdef | atthasmissing | attidentity | attisdropped | attislocal | attinhcount | attcollation | attacl | attoptions | attfdwoptions | attmissingval
----------+------------------------------+----------+---------------+--------+--------+----------+-------------+-----------+----------+------------+----------+------------+-----------+---------------+-------------+--------------+------------+-------------+--------------+--------+------------+---------------+---------------
17269 | a | 23 | -1 | 4 | 1 | 0 | -1 | -1 | t | p | i | f | f | f | | f | t | 0 | 0 | | | |
17269 | ........pg.dropped.2........ | 0 | 0 | 4 | 2 | 0 | -1 | -1 | t | p | i | f | f | f | | t | t | 0 | 0 | | | |
17269 | c | 23 | -1 | 4 | 3 | 0 | -1 | -1 | t | p | i | f | f | f | | f | t | 0 | 0 | | | |
(3 rows)

是的,第 2 列已删除。

让我们恢复它:

UPDATE pg_attribute
SET attname = 'b',
atttypid = 'integer'::regtype,
attstattarget = -1,
attisdropped = FALSE
WHERE attrelid = 'tab'::regclass
AND attnum = 2;

现在您应该再次看到该列。在删除该列后添加的所有行都具有该列的 NULL 值。

您必须添加列默认值以及在此过程中删除的其他内容,但我想您可以从备份中获取这些内容。

关于ruby-on-rails - 缺少 Postgres 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57850371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com