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 611513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:47:32+00:00 2026-05-13T17:47:32+00:00

To automatically add a column in a second table to tie it to the

  • 0

To automatically add a column in a second table to tie it to the first table via a unique index, I have a rule such as follows:

CREATE OR REPLACE RULE auto_insert AS ON INSERT TO user DO ALSO
INSERT INTO lastlogin (id) VALUES (NEW.userid);

This works fine if user.userid is an integer. However, if it is a sequence (e.g., type serial or bigserial), what is inserted into table lastlogin is the next sequence id. So this command:

INSERT INTO user (username) VALUES ('john');

would insert column [1, ‘john’, …] into user but column [2, …] into lastlogin. The following 2 workarounds do work except that the second one consumes twice as many serials since the sequence is still auto-incrementing:

CREATE OR REPLACE RULE auto_insert AS ON INSERT TO user DO ALSO
INSERT INTO lastlogin (id) VALUES (lastval());

CREATE OR REPLACE RULE auto_insert AS ON INSERT TO user DO ALSO
INSERT INTO lastlogin (id) VALUES (NEW.userid-1);

Unfortunately, the workarounds do not work if I’m inserting multiple rows:

INSERT INTO user (username) VALUES ('john'), ('mary');

The first workaround would use the same id, and the second workaround is all kind of screw-up.

Is it possible to do this via postgresql rules or should I simply do the 2nd insertion into lastlogin myself or use a row trigger? Actually, I think the row trigger would also auto-increment the sequence when I access NEW.userid.

  • 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-05-13T17:47:32+00:00Added an answer on May 13, 2026 at 5:47 pm

    Forget rules altogether. They’re bad.

    Triggers are way better for you. And in 99% of cases when someone thinks he needs a rule. Try this:

    create table users (
      userid serial primary key,
      username text
    );
    
    create table lastlogin (
      userid int primary key references users(userid),
      lastlogin_time timestamp with time zone
    );
    
    create or replace function lastlogin_create_id() returns trigger as $$
      begin
        insert into lastlogin (userid) values (NEW.userid);
        return NEW;
      end;
    $$
    language plpgsql volatile;
    
    create trigger lastlogin_create_id
      after insert on users for each row execute procedure lastlogin_create_id();
    

    Then:

    insert into users (username) values ('foo'),('bar');
    
    select * from users;
    
     userid | username 
    --------+----------
          1 | foo
          2 | bar
    (2 rows)
    
    select * from lastlogin;
    
     userid | lastlogin_time 
    --------+----------------
          1 | 
          2 | 
    (2 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have Visual Studio 2008 automatically add heading information to
What code do you need to add in PHP to automatically have the browser
Is it possible for a Symbian S60 application to automatically add itself to one
I use this line in my .htaccess file to automatically add a trailing slash
When creating a new file with vim, I would like to automatically add some
I'm using an automatically created (with wsdl.exe and the GUI-based Add web reference command)
Gmail automatically greys text that looks like a signature. Anyone have any guesses how
I have an issue when I try to use datagrid.items.add() to add items from
How do you automatically set the focus to a textbox when a web page
How can you automatically import the latest build/revision number in subversion? The goal would

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.