rails/rails — Issue #4313: Postgres Table Inheritance Support
Author: robertsosinski
Created: 2012-01-05 04:51:19+00:00
State: open

Hello,

It seems certain operations (finding by id, inserting, updating) has issues when using table inheritance. For example, I have the following three tables:

create table trades (
id serial primary key,
price money,
quantity integer,
created_at timestamp,
updated_at timestamp
);

create table stock_trades (
ticker varchar
) inherits(trades);

create table bond_trades (
cusip varchar
) inherits(trades);

All operations work correctly on the `trades` table, but for the `stock_trades` and `bond_trades` table, things get a bit finicky with the `id` field. I can do `StockTrade.where(:id => 1)`, or `StockTrade.find_by_id(1)`. However, if I do `StockTrade.find(1)`, the query fails with the following:

ERROR: zero-length delimited identifier at or near """" at character 68
STATEMENT: SELECT "stock_trades".* FROM "stock_trades" WHERE "stock_trades"."" = $1 LIMIT 1

You'll notice that it is not referencing the id in the where clause (being just an empty set of quotes).

Creating new records on `stock_trades` or `bond_trades` also yields a problem with the `id`. Creating a new stock trade with `StockTrade.create(:price => '33.33'.to_d, :quantity => 100, :ticker => 'VZ')` for example, fails with the following:

ERROR: null value in column "id" violates not-null constraint
INSERT INTO "stock_trades" ("created_at", "id", "price", "quantity", "ticker", "updated_at") VALUES ($1, $2, $3, $4, $5, $6);

The issue seems to be that Postgres trying to write to the `id` with a `null`, instead of leaving it out of the values list.

Updating also has problems as well. Doing `st = StockTrade.find_by_id(1); st.update_attribute(:quantity, 101)` fails with the following:

ERROR: zero-length delimited identifier at or near """" at character 110
STATEMENT: UPDATE "stock_trades" SET "quantity" = 101, "updated_at" = '2012-01-05 04:45:55.734441' WHERE "stock_trades"."" = 1

Overall, it seems that ActiveRecord is not seeing the `id` column, or is not identifying it as an id.