activerecord4-redshift-adapter icon indicating copy to clipboard operation
activerecord4-redshift-adapter copied to clipboard

Doesn't fetch the primary key

Open TMSCH opened this issue 10 years ago • 9 comments

Hi,

I'm trying to use the adapter in Rails 4 but there's a bug quite weird which prevent me from using it correctly.

When I'm creating a new record or fetching an existing one, it doesn't load its primary key. For instance I have a table like this:

Table--- id (int4) pkey ref_key(int4)

I'm fetching a record using ref_key:

Table.find_by(ref_key: 5)

And it returns a record:

table(id: (null), ref_key: 5)

where the primary key id hasn't been loaded.

Is there someone else experiencing the same problem? If not, do you have any idea why I could have this problem? Can it come from the table definition in Redshift? (the id is using an Identity for instance)

Thank you! I'd be happy to help updating the adapter if necessary.

TMSCH avatar Apr 04 '14 11:04 TMSCH

Yes, I met exactly same trouble. That seems ActiveRecord's own feature (or bug).

Did you set PRIMARY KEY restriction on the id column? If not, ActiveRecord sets nil for that column.

Pasting CREATE TABLE statement here may be helpful...

aamine avatar Apr 04 '14 12:04 aamine

Thank you for replying so quickly!

I have indeed set PRIMARY KEY constraint on the id column...

Here is the statement:

CREATE TABLE thomas.tests
(id INT IDENTITY(1,1) not null,
ref_key INT,
start_date TIMESTAMP,
end_date TIMESTAMP,
primary key(id));

How did you manage to overcome this issue?

TMSCH avatar Apr 04 '14 12:04 TMSCH

Mysterious. In my situation, I just forgot PRIMARY KEY.

I have tried your code, but I got id successfully... Hmmm.

SQL

=> CREATE TABLE tests
(id INT IDENTITY(1,1) not null,
ref_key INT,
start_date TIMESTAMP,
end_date TIMESTAMP,
primary key(id));
CREATE TABLE
=> insert into tests (ref_key, start_date, end_date) values (4, timeofday()::timestamp, timeofday()::timestamp);
INSERT 0 1

Rails Console

% bundle exec rails console
Loading development environment (Rails 4.0.2)
[1] pry(main)> Test.find_by(ref_key: 4)
  Test Load (2512.0ms)  SELECT "tests".* FROM "tests" WHERE "tests"."ref_key" = 4 LIMIT 1
=> #<Test id: 1, ref_key: 4, start_date: "2014-04-04 13:51:36", end_date: "2014-04-04 13:51:36">

aamine avatar Apr 04 '14 14:04 aamine

You're right : when I do find_by it works. But when I create a new record, it doesn't fetch the id of the newly created record...

TMSCH avatar Apr 07 '14 14:04 TMSCH

As to finding and fetching, maybe the issue I just filed is related to this one: https://github.com/aamine/activerecord4-redshift-adapter/issues/16

JohnB avatar Jun 16 '15 15:06 JohnB

+1

elioncho avatar May 19 '18 04:05 elioncho

@TMSCH/@elioncho got any fix for it?

dhanabalt avatar Feb 14 '19 14:02 dhanabalt

It's way back... I don't think I had a fix for this.

TMSCH avatar Feb 21 '19 00:02 TMSCH

okay Thanks @TMSCH

dhanabalt avatar Feb 21 '19 06:02 dhanabalt