Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't fetch the primary key #7

Open
TMSCH opened this issue Apr 4, 2014 · 9 comments
Open

Doesn't fetch the primary key #7

TMSCH opened this issue Apr 4, 2014 · 9 comments

Comments

@TMSCH
Copy link

TMSCH commented Apr 4, 2014

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.

@aamine
Copy link
Owner

aamine commented Apr 4, 2014

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...

@TMSCH
Copy link
Author

TMSCH commented Apr 4, 2014

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?

@aamine
Copy link
Owner

aamine commented Apr 4, 2014

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">

@TMSCH
Copy link
Author

TMSCH commented Apr 7, 2014

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...

@JohnB
Copy link

JohnB commented Jun 16, 2015

As to finding and fetching, maybe the issue I just filed is related to this one: #16

@elioncho
Copy link

+1

@dhanabalt
Copy link

@TMSCH/@elioncho got any fix for it?

@TMSCH
Copy link
Author

TMSCH commented Feb 21, 2019

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

@dhanabalt
Copy link

okay Thanks @TMSCH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants