无法使用ActiveRecord(没有Rails)更新记录。

huangapple go评论66阅读模式
英文:

Cannot update record with ActiveRecord (no rails)

问题

你好,以下是你要翻译的内容:

CREATE TABLE user_logins (token VARCHAR(512) NOT NULL, user_id UUID UNIQUE NOT NULL, create_date TIMESTAMPTZ DEFAULT clock_timestamp(), last_visit TIMESTAMPTZ DEFAULT clock_timestamp(), expire TIMESTAMPTZ DEFAULT clock_timestamp() + INTERVAL '30 DAYS');
require "active_record"

class UserLogin < ActiveRecord::Base

end
  def update_last_visited(user_token)
    user_login = UserLogin.find_by(token: user_token)

    if user_login == nil
      return false
    end

    user_login.update(last_visit: Time.now.utc)
  end
2023-02-19 11:55:21 - ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR:  zero-length delimited identifier at or near ""
LINE 1: ...logins" SET "last_visit" = $1 WHERE "user_logins"." IS NULL
require "active_record"
require "yaml"
require 'securerandom'

db_config_file = File.open("database.yml")
db_config = YAML::load(db_config_file)
ActiveRecord::Base.establish_connection(db_config)

class UserLogin < ActiveRecord::Base

end

def update_last_visited(user_token)
  user_login = UserLogin.find_by(token: user_token)

  if user_login == nil
    return false
  end

  user_login.update(last_visit: Time.now.utc)
end

user_login = UserLogin.new
user_login.token = "wompas"
user_login.user_id = SecureRandom.uuid
user_login.save
update_last_visited(user_login.token)
adapter: 'postgresql',
host: 'localhost',
port: 5432
username: 'user',
password: 'password',
database: 'dbname'

注意:该项目不使用Rails,仅使用ActiveRecord作为ORM工具。

英文:

Hello I have the following table in my PostgreSQL database:

CREATE TABLE user_logins (token VARCHAR(512) NOT NULL, user_id UUID UNIQUE NOT NULL, create_date TIMESTAMPTZ DEFAULT clock_timestamp(), last_visit TIMESTAMPTZ DEFAULT clock_timestamp(), expire TIMESTAMPTZ DEFAULT clock_timestamp() + INTERVAL &#39;30 DAYS&#39;);

For which I have the following ActiveRecord class

require &quot;active_record&quot;

class UserLogin &lt; ActiveRecord::Base

end

I want to update the last_visit column, which I try to do like this:

  def update_last_visited(user_token)
    user_login = UserLogin.find_by(token: user_token)

    if user_login == nil
      return false
    end

    user_login.update(last_visit: Time.now.utc)
  end

However, I get this error:

2023-02-19 11:55:21 - ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR:  zero-length delimited identifier at or near &quot;&quot;&quot;&quot;
LINE 1: ...logins&quot; SET &quot;last_visit&quot; = $1 WHERE &quot;user_logins&quot;.&quot;&quot; IS NULL

As I have understood ActiveRecord if I have the object ActiveRecord should be able to update the record, or have I miss understood something?
Should I instead do an update based on a where?

A minimal reproducible example:

require &quot;active_record&quot;
require &quot;yaml&quot;
require &#39;securerandom&#39;

db_config_file = File.open(&quot;database.yml&quot;)
db_config = YAML::load(db_config_file)
ActiveRecord::Base.establish_connection(db_config)


class UserLogin &lt; ActiveRecord::Base

end

def update_last_visited(user_token)
  user_login = UserLogin.find_by(token: user_token)
  
  if user_login == nil
    return false
  end
  
  user_login.update(last_visit: Time.now.utc)
end


user_login = UserLogin.new

user_login.token = &quot;wompas&quot;
user_login.user_id = SecureRandom.uuid

user_login.save

update_last_visited(user_login.token)

The database.yml file:

adapter: &#39;postgresql&#39;,
host: &#39;localhost&#39;,
port: 5432
username: &#39;user&#39;,
password: &#39;password&#39;,
database: &#39;dbname&#39;

Note: The project does not utilise Rails, we only use ActiveRecord for its ORM capabilities.

答案1

得分: 2

你的 UserLogin 没有主键。但是 ActiveRecord 期望表默认有一个名为 id 的主键列。请参考官方 Rails 指南中的 Active Record Basics / Schema Conventions

在你的示例中,似乎 user_id 可以被用作主键,因为它不可以是 NULL 并且必须是唯一的。你可以尝试像这样配置 ActiveRecord 以使用 user_id 作为主键。

class UserLogin < ActiveRecord::Base
  self.primary_key = :user_id
end

但总的来说,我建议不要违反默认的约定,最好只是在该表中添加一个 bigint 类型的 id 列。虽然可以配置自定义的主键列,但这会使一切变得更加复杂,因为你需要在其他用例中记住这些自定义设置,比如在定义关联时。

英文:

Your UserLogin doesn't have a primary key. But ActiveRecord expects tables to have a primary key column named id per default. See Active Record Basics / Schema Conventions in the official Rails Guides.

In your example, it feels like the user_id can be used as a primary key, because is cannot be NULL and it must be unique. You can try to configure user_id to be used by as primary key by ActiveRecord like this.

class UserLogin &lt; ActiveRecord::Base
  self.primary_key = :user_id
end

But in general, I suggest not fighting the default conventions and would just add a bigint id column to that table. While it is possible to configure custom primary key columns, it makes everything a bit more complex because you will need to remember those custom setting in other use cases – like, for example, when defining association.

huangapple
  • 本文由 发表于 2023年2月19日 19:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定