Rails 3.2 Postgres Save Error "Activerecord::Statementinvalid: Pg::Error: Error: Syntax Error Near 'T' at Position 5"

Rails 3.2 Postgres Save Error ActiveRecord::StatementInvalid: PG::Error: ERROR: Syntax error near 'T' at position 5

I just had a similar problem, on Rails 3.2. If you're not using Rails trunk (working towards 4.0), it doesn't understand the hstore natively - you need to define a serialization coder (which is provided by the activerecord-postgres-hstore gem), like so:

class Example < ActiveRecord::Base
serialize :data, ActiveRecord::Coders::Hstore
end

Invalid statement with Postgres hstore and rails

Adding the code " serialize :data, ActiveRecord::Coders::Hstore" from Rails 3.2 Postgres Save Error "ActiveRecord::StatementInvalid: PG::Error: ERROR: Syntax error near 'T' at position 5" solved my issue. I just had the wrong model name when I initially tried this as a fix.

Ruby on Rails can't update pg database table values

changing model in the following way solves the problem

class SomeUser < ApplicationRecord
self.primary_key = "id"
end

Syntax error near = when running pg_upgrade 9.4 to 9.5

The other solutions either were not options, or they didn't work.

The solution that worked for me was:

ALTER EXTENSION hstore UPDATE TO '1.1';

Source: https://www.postgresql.org/message-id/22170.1457479307%40sss.pgh.pa.us

dependant: :destroy leading to postgresql error

For anyone coming across this, what probably occurred is the following.

- create a join table via the rails shortcut has_and_belongs_to_many
- realise that you need later a has_many ... through: set-up

- chug along creating new join records actually runs... because the [...]_id is being referenced.

– come delete time, you're looking for an id of the join table and naturally that is nil

Error when modifying Postgres JSON field in Rails 3.2

I think that you're correct that ActiveRecord in Rails 3 doesn't natively support JSON. The cause of your error is that given a complex data type (in this case a Hash), ActiveRecord will first serialize it to a string, and the default serialization is YAML. That's what the --- in your error message is from—it's the YAML header.

A quick solution is to convert the Hash to JSON before assigning it to your attribute:

hsh = { name: "John" }
b.meta = hsh.to_json
b.save

This will get tedious fast, though. Instead, you can tell ActiveRecord to use JSON instead of YAML when serializing this attribute:

class Block < ActiveRecord::Base
serialize :meta, JSON
end

Rails ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table

After much debugging, I found the issue to be multiples :-(.

First, the joins order must match the includes order. Therefore, the 2 lines, doesn't produce the same SQL query. Naming aliases are different. I believe this is an active record \ arel issue that I'll flag.

Event.joins{entity.outer}.joins{place.outer}.includes(:place).includes(:entity)

is different than

Event.joins{entity.outer}.joins{place.outer}.includes(:entity).includes(:place)

However, this problem was transparent, up until I added another scope for my text search. My guess is because the SQL statement changed to add a column alias for every columns. The alias used the table alias as well, which was then different than the one in the FROM and JOIN clauses.

Changing the order, did not fix all my problems however. I ended up finding another issue where the SELECT clause of my texticle full text search gets ripped off from the query going to the DB. Instead, I get this error:

ActiveRecord::StatementInvalid: PG::Error: ERROR:  column "rank0.8601929506100121" does not exist
LINE 1: ...o_tsquery('english', 'canadiens'::text)) ORDER BY "rank0.860...

I also believe this to be either a texticle, squeel or ActiveRecord/Arel bug, not sure which one yet...

So painful I want to write up my own SQL statements by hand again...



Related Topics



Leave a reply



Submit