484
questions
4
votes
1
answer
810
views
How do I migrate an ActiveRecord model attribute from json to jsonb?
What should the migration look like? I would like to take advantage of the jsonb array querying technique.
0
votes
1
answer
2k
views
LPAD & RPAD with parameter
I have a problem, I have a function in postgres, to generate headers that will be used to generate a more complex report.
There all good. For the structure of the header, I leave spaces left or right ...
0
votes
0
answers
105
views
In postgresql , I am getting duplicates in a schema when I restore some tables in to a other schema in the same db .why?
In postgres 9.5 , I have a db called test. where I have 2 schemas
1.public
2. testcase
when i try to restore tables from public to testcase shema. I creates duplicates in public schema and I couldn't ...
5
votes
3
answers
33k
views
How to Connect to Docker Postgres Container from Host Machine
I put together a Rails dev environment by following instructions from
https://docs.docker.com/compose/rails/
It works, but I am unable to connect to the Postgres server from the host machine. I am ...
0
votes
1
answer
53
views
Issues with understanding multiple joins
I have the following tables:
users
____________
| id |
| first_name |
|____________|
pre_task_plans
____________
| id |
| creator_id | (fk user_id)
|____________|
...
0
votes
1
answer
882
views
Backup taken from pgadmin is smaller than backup taken from pgdump
Hello experts I am using postgres 9.5 . When I take a backup from pgadmin it has 950 MB size but when i take the same db backup from pgdump.exe command the backup size is with 7.5 GB. I am confused ...
0
votes
1
answer
386
views
Rails Tenant schema switching with background jobs not working
Setting up a scheduled rake task using Whenever gem.
Required to switch to a specific schema (Using Apartment gem in my project)
Code in config/schedule.rb
env :PATH, ENV['PATH']
env :GEM_PATH, ENV[...
1
vote
1
answer
120
views
update query not working on Heroku using cloud 9 command line
I have the following tables:
and the users table contains the following columns:
I am trying to run the following statement:
UPDATE users SET isTeacher = 'TRUE' WHERE id = '2';
but I am getting ...
0
votes
1
answer
92
views
Find conversations between users
I need an ActiveRecord query to find conversations for current_user and any another user
Conversations can have many users. I need to set users ids in that query(it can be two or more users).
class ...
0
votes
1
answer
61
views
How can I calculate locationGeo space in pgadmin 3 type with PostGIS functions?
My table name is map and I have latitude(double),longitude(double) and locationGeo(geometry) columns. I want to use a function to automatically compute the LocationGeo value using the given latitude ...
1
vote
1
answer
967
views
Postgres Trigger in specified time
Hello expert I am working in banking software vendor company and we are working in PostgreSQL database due to load of work in office time(pick hour) I want to execute some function in specified time(...
1
vote
1
answer
602
views
How to send PG binary data from the Rails controller?
I have a Postgres database with PostGIS extension and some geo-data in it. I need to generate tiles with custom SQL queries (using ST_... functions), that returns binary data (bytea, like \...
1
vote
1
answer
335
views
Why is Postgres double-quoting the first function identifier when I set a default function for a column?
I have a legacy table with a default expression in a Rails project. This expression picks a random four-digit number and converts it to a string. During my tests I have been typing this exact ...
1
vote
2
answers
1k
views
Why does this Postgres Select Query work in the local Docker Container but not on Amazon RDS Postgres?
My team has a local development environment that contains a Ruby on Rails app that connects to a Postgres database to retrieve some information about airports and use that to make further queries. ...
0
votes
1
answer
56
views
Rails ActiveRecord Behaviour?
I wanted to understand how ActiveRecord works in the following scenario.
@messages = Message.joins(:users).where("some condition").uniq
now if i use @messages.sum(:url_view_count)
I see the query is ...
1
vote
3
answers
13k
views
SQL Count digits in a string
I have a table of the following information.
description: adjkfa34kj34kj33j4k3jk4
description: adfkjjkdf34_3434kjkjkj3
description: akjdfkjadfkjadkjfkjadfj
description: 34394394093040930949039
How ...
0
votes
1
answer
942
views
Save a list of objects in a transaction operation
I'm trying to save in my postgres database a list of objects from a excel file. It must be done inside a transaction.
From the code below I can't rollback the transaction from invalid cases. The ...
1
vote
1
answer
2k
views
Connecting to localhost PostgreSQL on Mac using PSequel GUI?
I have used Sequel Pro as a GUI to connect to databases with previous projects which were done using Laravel 5.4. It seemed simple enough to connect to the database using the Sequel Pro GUI. I have ...
1
vote
1
answer
217
views
Postgres- pgsql taking more time to retrieve data from table with more than 1.5 billion rows
How can I optimize the table or the query the following pgsql query(takes 34 min. to get 770 records)? Already indexes are added to the table for few columns. Not sure what else can make this query
...
1
vote
1
answer
43
views
How to achieve concept of connection list with minimal cost using rails ActiveRecord?
Say User A is connected to B, then user B is automatically considered to be connected with user A.
User has_many connection
Connection Table:
id, user_id, connected_user_id
Say
id || user_id|| ...
0
votes
1
answer
85
views
Rails dropping last value in array when saving to Postgres database as JSON array
I am building a web app using Rails and Postgres. I am trying to save an array into my database table and it is only deleting the last value in the array.
For example if my array is:
a= [10,12,14,16]...
0
votes
1
answer
563
views
Sort results in varchar column with Postgres [duplicate]
I have theses values in my table
id | NAME
1 | 20 MEGA
2 | 30 MEGA
3 | 10 MEGA
4 | 300 MEGA
5 | 100 MEGA
6 | 25 MEGA
What i want:
id | NAME
3 | 10 MEGA
1 | 20 MEGA
6 | 25 MEGA
2 | 30 MEGA
5 | 100 ...
0
votes
2
answers
58
views
How do I match a database ID when pulling data from a database with an ID that needs to match a text string?
If the question isn't clear enough, I'm saving, for example, states in a database with an ID. I can return that ID but then I'm having difficulty turning it into the state name, for instance "...
0
votes
1
answer
88
views
How do I automatically generate a new row in a table when the user signs up?
I'm trying to edit the devise sign up flow so that when a new user signs up I automatically add a row to a specific table with all default values. What is the best way to do this?
Some thought ...
0
votes
1
answer
519
views
How do I create a foreign key in rails 5 if I've already created the models for the tables I want to associate?
All of the references I've found either show me how to do it upon table creation, or are for a much earlier version of rails. Ideally, I'd like like the foreign_key to be named 'author_id' in the ...
0
votes
1
answer
318
views
how to efficently use group_by using rails
I have tables records like below:
I am grouping them based on by am, pm and none.
What I really need to break down the group_by for none to 7 7 columns as my total records of none are 14.
Query I ...
0
votes
1
answer
884
views
ActiveRecord error when running rails console
When I launch rails console and want to test a model template I just created, rails console failed to create my new model instance.
yellow:maw thierry$ rails console
Running via Spring preloader in ...
0
votes
1
answer
46
views
How can I use value of the id column as default value of another non-nullable column when using postgresql-rails?
Here's one example where such a requirement comes up:
Suppose we have a table 'sample' in our database which has a column 'col1' and an auto-incremental 'id' column.
We want the default value of '...
1
vote
0
answers
71
views
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.range_to_string gone from Rails 4.1 -> 4.2
ActiveRecord::ConnectionAdapters::PostgreSQLColumn.range_to_string gone from Rails 4.1 -> 4.2
My searches on the web were so fruitfull that I finished hardcoding the 4.1 version into our code, and I ...
0
votes
2
answers
460
views
Push new table to heroku without having to reset existing database
I'm at the early stages of learning RoR and working with Heroku. So i have a fledging app with local and production databases. I've used pg:push to create the initial production database but I ...
8
votes
1
answer
2k
views
Substring search with pg_search
I'm using pg_search to search for Products:
class Product
include PgSearch
pg_search_scope(
:search_for,
against: %i(sku),
associated_against: { translations: [:name, :description] },...
0
votes
0
answers
62
views
Randomly degrading heroku DB performance?
So I am working on a pretty high traffic rails/heroku/postgres app, the backend only, and after running for hours, days, or weeks at times the database will randomly start taking 120 seconds to ...
3
votes
2
answers
1k
views
How to group and count the last N results?
In Rails, you can use:
Model.group(:field).count
to yield something like:
{"a"=>7, "b"=>5, "c"=>3 "d"=>3, "e"=>4}
But how can I count ONLY in the last N lines, not the entire table, with the ...
4
votes
2
answers
4k
views
Postgres conditional unique constraint
Pretend I have a users table where users are members of a specific tenant, and their e-mails are uniquely indexed to their tenant, like this:
User
id | tenant_id | email
1 1 ...
2
votes
1
answer
388
views
Add Current User id to trigger sql query hair_trigger gem rails
I am using hair_trigger gem and in my model, i want to keep changes to some columns.
trigger.after(:insert).declare("#{current_user.id} as current_user_id") do
"insert into histories (item_type, ...
0
votes
2
answers
2k
views
How to order records by created_at last 3 days first, then by another column, using rails [Postgresql]
Using Active Record, How to order by 2 columns (created_at, views):
1st: the records created within the last 3 days
2nd: everything after that should be ordered only by views
In order to get a ...
0
votes
1
answer
185
views
Correct associations and database design without (single table) inheritance for Rails-Ember
I'm struggling to figure out how to design my PostgreSQL database tables and Rails API models such that their associations can be implemented 1:1 in my Ember front-end, whereby Ember and Rails can ...
0
votes
1
answer
270
views
In Rails 5, How to create a controller action which serves some binary data?
Today I am working with Keras.js which has a repo here:
https://github.com/transcranial/keras-js
To operate this I need to serve some JS which looks something like this:
const model = new KerasJS....
4
votes
1
answer
5k
views
postgresql 9.3 log slow queries
I am using Rails application with Postgres 9.3 database and doing some performance fixes. I would like to know how to find out slowest queries in log files. let say queries taking more then 30ms.
2
votes
1
answer
38
views
DB migrate scripts break on references
I've been following online examples of how to create tables the Rails way and I noticed the migration scripts I created containing references don't work out of the box. I thought maybe I did something ...
1
vote
1
answer
27
views
How to list records of a Table with a counter of how many are used through a relationship
I have these 2 tables:
create_table "instruments", force: :cascade do |t|
t.string "name"
...
create_table "user_instruments", force: :cascade do |t|
t.integer "user_id"
t....
11
votes
1
answer
16k
views
Can Postgres silently ignore column constraint conflicts?
I have a Postgres 9.6 table with certain columns that must be unique. If I try to insert a duplicate row, I want Postgres to simply ignore the insert and continue, instead of failing or aborting. If ...
0
votes
0
answers
118
views
Why HABTM relation entries are not cleared after a destroy in Rails?
I was wondering why I have to write this (following) code to clear the relation table after destroying one of the two elements joined in a HABTM relation in Rails (using 5 here).
class Model < ...
1
vote
1
answer
937
views
PostgreSQL-Data File
Which process writes the data file in PostgreSQL?
And what are the data files in postgreSQL?
Note: Performing Insert/Update/Delete operation on postgreSQL-9.5. I want to verify which Process is ...
30
votes
8
answers
30k
views
Rails 5 how to clear or delete production postgres database
I am trying to delete a production database so I can start fresh. When I upgraded to rails 5 from rails 4, it is now protecting the production database from accidental deletion. It shows the following ...
1
vote
1
answer
311
views
What validations to use for an array data type
PostgreSQL adds other data types to the Active Record default.
See PostgreSQL documentation, List of available datatypes at Stackoverflow and Active Record and PostgreSQL at RailsGuides.
For the ...
1
vote
1
answer
1k
views
PG::UndefinedTable: ERROR: relation does not exist for Multitenant Rails App
I am working on a Multi-tenant Ruby on Rails App along with another developer. I pulled his branch and received new migration files. I ran rake db:migrate. I visit the index of that controller and I ...
1
vote
1
answer
758
views
geocode 2 addresses with geocoder gem
I'm trying to geocode 2 addresses which are asked in 1 form.
<%= f.input :kotadres %>
<%= f.input :location %>
Both of them work with Google Autocomplete like this:
<script>
...
0
votes
2
answers
3k
views
Postgresql, Rails - could not fork autovacuum worker process: Resource temporarily unavailable
This is happening to me while in my local environment, Mac OSX, every time I start my server - puma - and workers - resque.
The logs don't say anything helpful, just a repeated, "could not fork ...
5
votes
4
answers
349
views
Group_by - Ruby/Rails + Postgres
I am pretty new to ROR and Postgre and i'm in trouble to achieve this.
I have a Working_hour Model and a Merchant Model, where merchant has_many working_hours and working_hour belongs to Merchant. ...