有没有办法在PostgreSQL中编写不区分大小写的查询,例如我希望以下3个查询返回相同的结果.
SELECT id FROM groups where name='administrator'
SELECT id FROM groups where name='ADMINISTRATOR'
SELECT id FROM groups where name='Administrator'
Run Code Online (Sandbox Code Playgroud) 我们正在建立一个介绍博客.到数据库课程项目.
在我们的博客中,我们希望能够Labels开启Posts.它Labels本身不存在,只有与a相关时才会存在Posts.这样,Labels任何不使用的都不Posts应该留在数据库中.
不止一个Label可以属于单个Post,而且多个Post可以使用一个Label.
我们正在使用SQLite3(本地/测试)和PostgreSQL(部署).
下面是我们用来创建这两个表的SQL(SQLite3风格)以及关系表:
CREATE TABLE IF NOT EXISTS Posts(
id INTEGER PRIMARY KEY AUTOINCREMENT,
authorId INTEGER,
title VARCHAR(255),
content TEXT,
imageURL VARCHAR(255),
date DATETIME,
FOREIGN KEY (authorId) REFERENCES Authors(id) ON DELETE SET NULL
)
Run Code Online (Sandbox Code Playgroud)
CREATE TABLE IF NOT EXISTS Labels(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255) UNIQUE,
-- This is not working:
FOREIGN …Run Code Online (Sandbox Code Playgroud) 我正在阅读有关 postgres 方言上的 upsert 操作的 SqlAlchemy 文档,网址为http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert
有没有办法知道更新插入是插入还是更新?
该文档似乎省略了这个细节。
我正在尝试创建一份留下评论的独特患者名单.代码字很好,直到我上传到heroku,它在postgresql中不起作用.
这是我创建列表的Ruby .erb代码:
<% @comments.group(:patient_id).each_with_index do |comment, index| %>
<div class="profile-post color-one">
<span class="profile-post-numb"><%= index+1 %></span>
<div class="profile-post-in">
<h3 class="heading-xs"><a><%= link_to "#{comment.patient.first_name} #{comment.patient.last_name}", comment_path(comment) %></a></h3>
<p>Lastest comment from <%= time_ago_in_words(comment.created_at) %> ago<i class="pull-right"><%= link_to "Edit", edit_comment_path(comment) %> <%= link_to "Delete", comment_path(comment), method: :delete, data: {confirm: 'Are you sure you want to delete'} %></i></p>
</div>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
@comments在控制器中定义为:
def index
@comments = current_clinician.comments.order("created_at desc")
end
Run Code Online (Sandbox Code Playgroud)
heroku日志给我这个错误信息:
Run Code Online (Sandbox Code Playgroud)PG::GroupingError: ERROR: column "comments.id" must appear in the GROUP BY clause or be …
如果我有一个数字(例如88)并且我想在主ID列的Rails中执行LIKE查询以返回在ID结尾处包含该数字的所有记录(IE:88,288等),我该怎么办?这是生成结果的代码,在SQLLite中可以正常工作:
@item = Item.where("id like ?", "88").all
Run Code Online (Sandbox Code Playgroud)
在PostgreSQL中,我遇到了这个错误:
PG::Error: ERROR: operator does not exist: integer ~~ unknown
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?我已经尝试将数字转换为字符串,但这似乎也不起作用.
用户可以键入名称,系统应与文本匹配,即使用户输入或数据库字段包含重音(UTF-8)字符也是如此.这是使用该pg_trgm模块.
代码类似于以下内容:
SELECT
t.label
FROM
the_table t
WHERE
label % 'fil'
ORDER BY
similarity( t.label, 'fil' ) DESC
Run Code Online (Sandbox Code Playgroud)
当用户键入时fil,查询匹配filbert但不匹配filé powder.(因为重音字符?)
我试图实现一个unaccent函数并将查询重写为:
SELECT
t.label
FROM
the_table t
WHERE
unaccent( label ) % unaccent( 'fil' )
ORDER BY
similarity( unaccent( t.label ), unaccent( 'fil' ) ) DESC
Run Code Online (Sandbox Code Playgroud)
这只返回filbert.
建议:
CREATE EXTENSION pg_trgm;
CREATE EXTENSION unaccent;
CREATE OR REPLACE FUNCTION unaccent_text(text)
RETURNS text AS
$BODY$
SELECT unaccent($1); …Run Code Online (Sandbox Code Playgroud) 这是我的routes.rb
namespace :api do
get 'suggestions/jobs', to: "suggestions#jobs"
end
Run Code Online (Sandbox Code Playgroud)
我的控制器
class Api::SuggestionsController < ApplicationController
def jobs
@jobs = Job.job_title_search(params[:q]) #.select(:title, :label).distinct
if @jobs.present?
render json: @jobs, status: :ok
else
render json: "Not Found", status: :ok
end
end
end
Run Code Online (Sandbox Code Playgroud)
和模型
def self.job_title_search(q)
where('title LIKE ?', "%#{q}%").select(:title, :label).distinct
end
Run Code Online (Sandbox Code Playgroud)
在开发环境中就像localhost:3000/api/suggestions/jobs?q=dev数据样本一样
[
{"id":null,"title":"React Native Developer","label":"Top label job"},
{"id":null,"title":"Android Developer","label":"Top label job"},
{"id":null,"title":"Business Development Representative","label":"Mid label job"},
{"id":null,"title":"Node.js Developer","label":"Top label job"}
]
Run Code Online (Sandbox Code Playgroud)
这意味着它正在工作,但是当我推进Heroku时,就像example.herokuapp.com/api/suggestions/jobs?q=dev它正在显示
SyntaxError:JSON.parse:JSON数据的第1行第1列的意外字符
我知道代码是"Not Found"
render json: "Not Found", …Run Code Online (Sandbox Code Playgroud) 我目前有一个Rails项目,我将其部署到使用postgres数据库的生产服务器上。我在Windows中开发我的rails项目,这意味着,如果要在本地进行测试,则必须将database.yml文件中的所有数据库从postgres更改为sqlite3(因为将Windows设置为运行postgres服务器似乎是痛苦)。
我想做的是格式化我的database.yml像这样:
development:
adapter: postgresql
encoding: utf8
database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
pool: 5
username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: …Run Code Online (Sandbox Code Playgroud) database sqlite ruby-on-rails ruby-on-rails-3 rails-postgresql
postgresql ×7
ruby ×3
sqlite ×3
heroku ×2
sql ×2
activerecord ×1
database ×1
group-by ×1
many-to-many ×1
plpgsql ×1
python ×1
similarity ×1
sqlalchemy ×1
utf-8 ×1