Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:46:16+00:00 2026-06-10T02:46:16+00:00

I have a table with a timestamp field onto which I’ve created a composite

  • 0

I have a table with a timestamp field onto which I’ve created a composite index.

CREATE INDEX "IDX_NAME_A" ON TABLE "A" (a_id, extract(year FROM created_at))

I have another table which stores a year and a_id which I’d like to have a foreign key relation.

I can’t seem to find the syntax to do what I want.

ALTER TABLE "B" 
  ADD FOREIGN KEY(a_id, a_year) 
  REFERENCES A(a_id, extract(YEAR FROM created_at));

produces:

ERROR:  syntax error at or near "("

I’ve also tried …

ALTER TABLE "B"
  ADD FOREIGN KEY(a_id, a_year) 
  USING INDEX "IDX_NAME_A";

Any Ideas?

Table A
--------
a_id serial,
created_at timestamp default now()

Table B
-------
b_id serial
a_id integer not null,
a_year date_part('year')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:46:18+00:00Added an answer on June 10, 2026 at 2:46 am

    A foreign key constraint cannot reference an index. It has to be a table.
    A foreign key constraint cannot reference an expression. It has to point to column name(s) of the referenced table.
    And there has to exist a unique index (primary key qualifies, too, implicitly) on the set of referenced columns.

    Start by reading the manual about foreign keys here.


    The superior design would be to just drop the column b.a_year. It is 100% redundant and can be derived from a.created_at any time.

    If you positively need the column (for instance to enforce one row per year for certain criteria in table b), you can achieve your goal like this:

    CREATE TABLE a (
      a_id serial
     ,created_at timestamp NOT NULL DEFAULT now()
     ,a_year NOT NULL DEFAULT extract(year FROM now())::int -- redundant, for fk
     ,CHECK (a_year = extract(year FROM created_at)::int)
    );
    
    CREATE UNIQUE INDEX a_id_a_year_idx ON TABLE a (a_id, a_year); -- needed for fk
    
    CREATE TABLE b (
      b_id serial
     ,a_id integer NOT NULL
     ,a_year int -- doesn't have to be NOT NULL, but might
     ,CONSTRAINT id_year FOREIGN KEY (a_id, a_year) REFERENCES a(a_id, a_year)
    );
    

    Updated after @Catcall’s comment:
    The CHECK constraint in combination with the column DEFAULT and NOT NULL clauses enforces your regime.

    Alternatively (less simple, but allowing for NULL values) you could maintain the values in a.a_year with a trigger:

    CREATE OR REPLACE FUNCTION trg_a_insupbef()
      RETURNS trigger AS
    $BODY$
    BEGIN
    
    NEW.a_year := extract(year FROM NEW.created_at)::int;
    RETURN NEW;
    
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE;
    
    CREATE TRIGGER insupbef
      BEFORE INSERT OR UPDATE ON a
      FOR EACH ROW EXECUTE PROCEDURE trg_a_insupbef();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a field: 'last_modified - timestamp NOT NULL' I set
I want to add expire TIMESTAMP in DB table. I have one field timestamp
Say I have a 'user_log' table with the following field: id user_id status_text timestamp
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
Let's say I have a very large MySQL table with a timestamp field. So
I have a timestamp field in my table. How do I delete records older
I have a problem extracting the year out of a mysql timestamp field. I
I need to create a timestamp field for a table who's rows need to
I have a table with timestamp field call EVENT_TS (so it's stored as GMT
First thing, my date format for the field is TIMESTAMP I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.