- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
当我尝试将特定类保存到数据库时,我的应用开始抛出错误。我不确定到底是什么原因导致这种情况开始发生 - 过去几天我一直遇到各种数据库问题....
无论如何,我的模型似乎工作正常(它正在正确计算所有 before_save 值),但随后它尝试保存到数据库并爆炸。
SQL (0.8ms) INSERT INTO "portfolios" ("correlation_matrix", "created_at", "data", "mean_return", "std_dev", "updated_at", "weights") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["correlation_matrix", "--- \n- - 1.0\n - -0.4873114574375062\n- - -0.4873114574375062\n - 1.0\n"], ["created_at", Sat, 16 Jun 2012 15:12:35 MDT -06:00], ["data", {"TSX"=>0.5, "VUSTX"=>0.5}], ["mean_return", #<BigDecimal:7fadb119b750,'0.488052381E-1',18(45)>], ["std_dev", #<BigDecimal:7fadb119b598,'0.7668705159 123244E-1',18(45)>], ["updated_at", Sat, 16 Jun 2012 15:12:35 MDT -06:00], ["weights", "--- \nTSX: 0.5\nVUSTX: 0.5\n"]]
抛出这个错误:
ActiveRecord::StatementInvalid: PG::Error: ERROR: Syntax error near 'T' at position 5
我不知道这个错误是什么意思(或者这个“T”是什么......),甚至不知道从哪里开始解决它......任何帮助将不胜感激。
我可以发布任何可能需要的信息来解决这个问题......
相关迁移:
create_table :portfolios do |t|
t.text :weights
t.decimal :mean_return, :precision => 15, :scale => 10
t.decimal :std_dev, :precision => 15, :scale => 10
t.text :correlation_matrix
t.hstore :data
t.timestamps
end
execute "CREATE INDEX portfolios_gin_data_hstore ON portfolios USING GIN(data);"
完整堆栈跟踪:
ActiveRecord::StatementInvalid: PG::Error: ERROR: Syntax error near 'T' at position 5
: INSERT INTO "portfolios" ("correlation_matrix", "created_at", "data", "mean_return", "std_dev", "updated_at", "weights") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/postgresql_adapter.rb:1164:in `get_last_result'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/postgresql_adapter.rb:1164:in `exec_cache'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/postgresql_adapter.rb:665:in `block in exec_query'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter. rb:20:in `instrument'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `exec_query'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/relation.rb:66:in `insert'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/persistence.rb:363:in `create'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/timestamp.rb:57:in `create'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/callbacks.rb:268:in `block in create'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:403:in `_run__772785567275930853__create__1186465801021498362__callbacks'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
... 11 levels...
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/validations.rb:50:in `save'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2. 6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in `transaction'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block in save'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/activerecord-3.2.6/lib/active_record/transactions.rb:240:in `save'
from (irb):33
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /Users/brandon/.rvm/gems/ruby-1.9.3-p194@myapp/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
更新 1根据要求提供一些模型代码。
宏观的东西:
attr_accessible :weights
serialize :correlation_matrix
serialize :weights
has_and_belongs_to_many :securities, :uniq => true
has_and_belongs_to_many :efficient_frontiers
before_validation :format_weights_tickers, :add_securities, :validate_weights, :set_weights_in_hstore, :build_correlation_matrix, :set_mean_return, :set_standard_deviation
validates_presence_of :mean_return, :std_dev, :securities, :correlation_matrix, :weights
validates_numericality_of :mean_return, :std_dev
validate :uniqueness_of_weights
before_destroy :check_for_dependent
scope :by_std_dev, :order => 'std_dev ASC'
我能找到的与 HStore 列有关的所有内容我真正使用 HStore 的目的是搜索特定的代码。这曾经有效......我不知道发生了什么变化。我可能已经升级了 Postgres(我正在摆弄自制软件)或者可能用 Rails 改变了一些东西。我确实尝试回到 3.2.3,但我遇到了同样的错误。
def self.find_by_hstore(search_key, search_value)
where("data @> (:key => :value)", :key => search_key.to_s, :value => search_value.to_s)
end
def set_weights_in_hstore
self.data = {} if self.data.nil?
weights.each_pair {|ticker, weight| self.data[ticker.to_s] = weight}
end
如果这太不连贯,我可以发布整个模型。让我知道!
更新 #2 - 我在其他地方也遇到了类似的错误....
在简单的用户注册后......
SQL (10.9ms) INSERT INTO "users" ("admin", "confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "data", "email", "encrypted_password", "last_email_at", "last_sign_in_at", "last_sign_in_ip", "name", "plan", "remember_created_at", "reset_password_sent_at", "reset_password_token", "selected_portfolio_id", "sign_in_count", "unconfirmed_email", "updated_at", "verified") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23) RETURNING "id" [["admin", false], ["confirmation_sent_at", Sat, 16 Jun 2012 20:37:07 MDT -06:00], ["confirmation_token", "7Xu15pMrV9zTNmofv8bD"], ["confirmed_at", nil], ["created_at", Sat, 16 Jun 2012 20:37:07 MDT -06:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["data", {"min_rebalance_spacing"=>90 days, "max_contact_frequency"=>7 days, "allowable_drift"=>5}], ["email", "joe@what.com"], ["encrypted_password", "$2a$10$HNUmlYmcVXbBsyZRFCAB7e8c5mf6S9UOdWr/ZCz10y5Sg4gOh8Zvq"], ["last_email_at", Sat, 16 Jun 2012 20:37:07 MDT -06:00], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["name", "Joe Blow"], ["plan", "basic"], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["selected_portfolio_id", nil], ["sign_in_count", 0], ["unconfirmed_email", nil], ["updated_at", Sat, 16 Jun 2012 20:37:07 MDT -06:00], ["verified", false]]
20:37:07 log.1 | [ef4a7d55fb30e8fb82ac6c860e674bfc] [127.0.0.1] PG::Error: ERROR: Syntax error near 'm' at position 5
20:37:07 log.1 | : INSERT INTO "users" ("admin", "confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "data", "email", "encrypted_password", "last_email_at", "last_sign_in_at", "last_sign_in_ip", "name", "plan", "remember_created_at", "reset_password_sent_at", "reset_password_token", "selected_portfolio_id", "sign_in_count", "unconfirmed_email", "updated_at", "verified") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23) RETURNING "id"
UDPATE 3
我的数据库/用户表似乎发生了一些奇怪的事情。在迁移中加载环境时出现以下错误:
rake aborted!
PG::Error: ERROR: relation "users" does not exist
LINE 4: WHERE a.attrelid = '"users"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
最佳答案
我刚刚在 Rails 3.2 上遇到了类似的问题。如果您不使用 Rails 主干(朝着 4.0 方向努力),它本身不理解 hstore - 您需要定义一个序列化编码器(由 activerecord-postgres-hstore
gem 提供) ,像这样:
class Example < ActiveRecord::Base
serialize :data, ActiveRecord::Coders::Hstore
end
关于ruby-on-rails - Rails 3.2 Postgres 保存错误 "ActiveRecord::StatementInvalid: PG::Error: ERROR: Syntax error near ' T' 在位置 5",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11067120/
我想知道如何使用 CaSTLe AR 来“播种”自动递增的主键值?例如,希望 Orders 表的主键从 10000 开始。这是 1. 可能的 2. 创建订单号的好解决方案吗? 也许有一种方法可以在不是
假设你想 records = Model.all records.to_a.map{|m| m.serializable_hash(:root => true)} 就像 to_json(:root =
我目前正在尝试开发一个插件/gem 来观察多个模型。理想情况下,观察者应该只用一个单例方法自动实例化...... class MyModel true observe ActiveRecord:
使用 Mysql 我可以使用 COALESCE仅更新表中为空的值。 我怎样才能用 Rails (ActiveRecord) 做到这一点? 我不想创建 if表中每一列的语句,我猜如果我将 ActiveR
我从 Yii2 开始,想要将一些内容保存到我的数据库中。这是我的模型: class Course extends ActiveRecord { public $name; public
我正在开始一个新项目,最近我发现了 caSTLe 项目 activerecord,这似乎是一个很棒的解决方案,但与此同时,它看起来确实是非常规的。我想知道,这种感觉是来自学习新东西(我应该习惯它)还是
不知何故,我总是在周五收到这些。 我之前的问题是关于同样的问题,但我现在可以缩小范围: 我一整天都在研究这个问题,试图理解它。我有一个带有 lock_version 列的表,如此指定: add_col
我正在使用表中缓存的关键字构建搜索。在表中查找用户输入的关键字之前,它会被规范化。例如,删除了一些标点符号,如“-”,并标准化了大小写。然后使用规范化的关键字来查找获取搜索结果。 我目前正在使用 be
我有以下类用于“项目”和“颜色”之间的多对多关系。 并且“项目”不应该有重复的“颜色”,例如:-如果“Item1”有“Blue”和“Red”,那么我们不能向“Item1”添加另一个“Red” 这是正确
我对 Yii2 事件记录处理关系属性的方式感到困惑。是否可以将事件记录对象链接到另一个对象而不先保存它? 例如,我想将图像作为 Logo 添加到公司记录中,但尚未决定是否应保存这两个记录。给定一家公司
我决定使用 Castle ActiveRecord 为客户制作一个系统,一切都很顺利,直到我发现交易不起作用,例如; TransactionScope t = new T
Yii2 的新特性。 只是试图从 ActiveRecord 查询中获得返回。我意识到使用 Yii2 约定可能有更简单的方法来做到这一点 public function actionGet_permis
我收到一个错误(在 Sinatra + ActiveRecord Heroku 上)数据库配置没有指定适配器。 根据一些研究,这似乎是因为 Heroku 预计在 rackup 期间不使用环境变量。 我
我正在尝试按“created_at”日期查找记录 - 数据库列类型为“日期时间”并且 我正在使用来自 jQuery 的 UI DatePicker 我的网址如下所示:“localhost:3000/u
我想问这个问题。但我认为 BlameableBehavior和 TimestampBevavior可能是需要这样做的好例子: 我必须做什么才能使属性只能从 ActiveRecord 对象的外部读取而不
这是一个古老的问题,其中给定一个具有“类型”、“品种”和“价格”属性的表,您可以获取每种类型的最低价格的记录。 在 SQL 中,我们可以做 this通过: select f.type, f.varie
这似乎是一个微不足道的问题,但是我能想到的所有明显的解决方案都有自己的缺陷。 我们想要的是能够为新记录设置任何默认的 ActiveRecord 属性值,以使其在验证之前和期间可读并且不干扰用于搜索的派
做关联方法,比如has_many定义的那些和 belongs_to利用 ActiveRecord::Relation ? 如果是这样,是否有可能获得 ActiveRecord::Relation正在使
如何将 MySQL 的 WHERE 转换为 ActiveRecord :condition? SELECT * FROM users WHERE LENGTH(users.last_name) [
当使用带有 ActiveRecord T4 模板的 SubSonic 3 时,生成的代码会显示许多关于 CLS 合规性、未使用的项目和缺少 GetHashCode() 实现的警告。 为了避免它们,我做
我是一名优秀的程序员,十分优秀!