0

我正在使用 ruby​​ 访问 postgresql 数据库并对其进行更新。我尝试更新的字段之一称为“updated_at”,属于不带时区的时间戳类型。我正在使用以下语句:

db.prepare('statement1', "INSERT INTO games (game_hash,team1,team2,team1score,team2score,time,update_at) VALUES ($1,$2,$3,$4,$5,$6,$7)");
update = Time.now.strftime("%Y-%m-%d %H:%M:%S");
db.exec_prepared('statement1', [myId, team1, team1score, team2, team2score, progress, update]);

更新变量的格式正确,因为我最初得到的时间戳字段错误格式不正确,但不再这样做。但是数据库显示 updated_at 字段为空!!所有其他字段均已正确填充。有谁知道这是什么原因造成的?

更新- 我添加了一个名为 last_update 的 datetime 类型的新列,并且使用该列代替,我的时间戳被正确添加到数据库中!我猜是因为数据库最初来自 rails activerecord,所以 update/created_at 字段以某种方式受到保护。感谢大家帮助解决这个问题,我将只使用我的新专栏。

4

1 回答 1

0

简单的解决方案是这样做:

db.prepare('statement1', "INSERT INTO games (game_hash,team1,team2,team1score,team2score,time,update_at) VALUES ($1,$2,$3,$4,$5,$6, now())");
db.exec_prepared('statement1', [myId, team1, team1score, team2, team2score, progress]);

为您节省一行代码,以及大量可能出错的事情。

现在,如果这不能解决问题,请检查触发器。如果它确实解决了问题,则可能是拼写错误、保留字等。如果它不能解决问题,那么其他东西就会弄乱插入语句和行存储之间的数据。

于 2013-05-08T13:41:25.123 回答