activerecord4-redshift-adapter
activerecord4-redshift-adapter copied to clipboard
Doesn't fetch the primary key
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.
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...
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?
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">
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...
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
+1
@TMSCH/@elioncho got any fix for it?
It's way back... I don't think I had a fix for this.
okay Thanks @TMSCH