我有一个导入器,它接收电子邮件列表并将它们保存到postgres数据库中.以下是无表导入器类中的代码片段:
query_temporary_table = "CREATE TEMPORARY TABLE subscriber_imports (email CHARACTER VARYING(255)) ON COMMIT DROP;"
query_copy = "COPY subscriber_imports(email) FROM STDIN WITH CSV;"
query_delete = "DELETE FROM subscriber_imports WHERE email IN (SELECT email FROM subscribers WHERE suppressed_at IS NOT NULL OR list_id = #{list.id}) RETURNING email;"
query_insert = "INSERT INTO subscribers(email, list_id, created_at, updated_at) SELECT email, #{list.id}, NOW(), NOW() FROM subscriber_imports RETURNING id;"
conn = ActiveRecord::Base.connection_pool.checkout
conn.transaction do
raw = conn.raw_connection
raw.exec(query_temporary_table)
raw.exec(query_copy)
CSV.read(csv.path, headers: true).each do |row|
raw.put_copy_data row['email']+"\n" unless row.nil? …Run Code Online (Sandbox Code Playgroud) 我试图在ruby中使用pg gem中的预处理语句.这就是我的陈述的样子
conn.prepare("insert_values", "insert into " + objectName + "(" + headerStr + ") values (" + prep_values + ")")
conn.exec_prepared("insert_values", arr)
Run Code Online (Sandbox Code Playgroud)
我一直在收到错误
准备好的语句insert_values已经存在.
我该如何解决??谢谢
这是尝试时的错误 bundle install
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/usr/bin/ruby1.8 extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. …Run Code Online (Sandbox Code Playgroud) 我花了好几个小时试图找出它为什么不起作用后终于设法得到'pg'宝石安装...
最后我进入了sudo env ARCHFLAGS="-arch x86_64" gem install pg -v 0.12.2 -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config一个像魅力一样的工作.但是现在我尝试捆绑时仍然有同样的错误 - 所以我猜我没有真正解决问题?无论如何,这bundle install是在说什么:
Installing pg (0.12.2)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/thomas/.rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb --with-pg-config=/usr/pgsql-9.2/bin/pg_config
Using config values from /usr/pgsql-9.2/bin/pg_config
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
sh: /usr/pgsql-9.2/bin/pg_config: No such file or directory
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconnectionUsedPassword()... yes
checking for PQisthreadsafe()... yes
checking for PQprepare()... yes
checking for …Run Code Online (Sandbox Code Playgroud) 怪事:
我正在使用postgres后端在rails 4.1中工作.我的一个模型有一些数组字段,我希望这些字段由'pg'gem(0.17.1)正确处理.我的迁移文件和生成的模式可以在下面找到.
有问题的字段是字符串数组.下面,我发布了迁移,生成的模式以及似乎不起作用的代码(以及在rails控制台中运行此代码时的输出).
用户模型中没有引用此字段的任何内容.
移民:
add_column :users, :test, :string, :array => true, default: '{}'
Run Code Online (Sandbox Code Playgroud)
模式:
t.string "test", default: [], array: true
Run Code Online (Sandbox Code Playgroud)
以下是我在rails控制台中重现此步骤的步骤(假设已创建基本用户模型来保存字段):
u = User.first()
u.test.push('testValue')
u # => #<User id: 1, name: "dave", passhash: nil, email: nil, created_at: "2014-10-04 10:12:29", updated_at: "2014-10-04 10:12:29", provider: "identity", uid: "1", oauth_token: nil, oauth_expires_at: nil, test: ["testValue"]>
u.save
User.find(1).test # => []
Run Code Online (Sandbox Code Playgroud)
因此,用户记录似乎已正确初始化 - 它为该字段设置了一个空数组 - 我可以对其进行数组操作.但不知何故,我无法进行任何修改.
仅供参考,我可以使用相同的程序修改和保存其他字段; 它只是表现得像这样的数组.
有任何想法吗?
编辑:
u.test.push('newitem')按照我的预期返回新数组.没有任何问题的迹象,或者validates_uniqueness_of有些错误 - 我会在控制台中看到这个问题吗?感谢第一条评论,我现在使用'更新'进行测试:
u.update(test: [(u.test << 'testValue')].flatten)
Run Code Online (Sandbox Code Playgroud)
给了我想要的行为,但它看起来很笨拙.我需要在模型中改变一些东西吗?我不确定我正在做什么使validates_uniqueness_of fire(它没有在模型中设置).
我对如何在node.js模块中正确实现postgres连接池知之甚少.
我的问题是:当我在需要连接到pg的每个模块中创建新池时,它是否有问题?
有没有办法为整个应用程序全局创建池?
谢谢.
使用pg在我申请的NodeJS我希望能够到一个数组传递给函数,这样我可以使用IN的语句作为我查询的一部分
例子
async function myMethod(array) {
// array looks like [ '83', '82', '54', '55', '79' ] for example
let response;
try {
response = await pool.query("SELECT * FROM fixtures WHERE id IN($1)", [array]);
} catch (e) {
console.log(e);
}
return response.rows;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
{ error: invalid input syntax for integer: "{"83","82","54","55","79"}"
Run Code Online (Sandbox Code Playgroud)
我该如何在此处构建我的查询?
我有一个rails应用程序,我试图在Amazon EC2实例中运行.此应用程序已在各种Linux,OS X和Windows系统上正确测试.
在EC2服务器上,我从源代码构建了PostgresSQL 9.首先,'pg'gem不会安装,但我使用以下命令构建它:
gem install pg -- --with-pgsql-lib=/usr/local/pgsql/lib --with-pgsql-config=/usr/local/pgsql/bin/pg_config
Run Code Online (Sandbox Code Playgroud)
这似乎构建得恰到好处.但是,当我运行rake任务时,例如'rake db:migrate',我收到以下错误:
rake aborted!
libpq.so.5: cannot open shared object file: No such file or directory - /home/ec2-user/.rvm/gems/ruby-1.8.7-p334/gems/pg-0.11.0/lib/pg_ext.so
Run Code Online (Sandbox Code Playgroud) 在使用rake任务填充数据库时,Hartl的Rails教程(第2版)第9章末尾遇到了错误.结束了它,但不知道出了什么问题.如果有其他人遇到这个错误,这就是我所做的.如果有人知道出了什么问题,请发表评论 - 我很想知道.谢谢!
跑这些命令
$ git push heroku
$ heroku run rake db:migrate
$ heroku pg:reset SHARED_DATABASE --confirm <name-heroku-gave-to-your-app>
$ heroku run rake db:populate
Run Code Online (Sandbox Code Playgroud)
然后,得到了这个错误:
rake aborted!
PGError: ERROR: relation "users" does not exist
: 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
Tasks: TOP => db:populate
(See full trace by running task …Run Code Online (Sandbox Code Playgroud) 刚刚做了一个简单的事,bundle update并且得到了一个与pgPostgreSQL 的gem 相关的恼人错误.我的系统上没有安装PostgreSQL(除非它是为Mac OS X 10.9.1自动安装的).这从来就不是问题.有任何想法吗?
Using jquery-rails (3.0.4)
Using jquery-rails-cdn (1.0.1)
Installing liquid (2.6.1)
Using paperclip (3.5.2)
Installing pg (0.17.1)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/Users/scott/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. …Run Code Online (Sandbox Code Playgroud) pg ×10
postgresql ×6
ruby ×3
arrays ×2
node.js ×2
activerecord ×1
amazon-ec2 ×1
bundler ×1
gem ×1
heroku ×1
import ×1
javascript ×1
linux ×1
macos ×1
node-modules ×1
rubygems ×1
ubuntu-12.04 ×1