In my application, I have an abstract base class, and 2 derived classes. The number of derived classed will probably not increase. If it does, it won't be more than a few.

The class structure looks like this:

abstract class Base
    int FirstParameter
    int SecondParameter
    abstract void DoSomeWork();

class Foo : Base
    int FoosParameter
    override void DoSomeWork(){...}

class Bar : Base
    override void DomeWork(){...}

Essentially, Bar only needs the data provided by Base, but does its own work on the data. From a data standpoint, Bar and Base are the same. Foo, on the other hand, has some extra data tacked on to it.

The database has 3 tables, one for Base, one for Foo, and for Bar. Since the number of subclasses is not going to grow significantly, it makes more sense to give each class its own table.


base_id PK,
first_parameter int not null,
second_parameter int not null,
foo_id int,
bar_id int


foo_id PK,
foos_parameter int not null,


bar_id PK

tblBar consists of 1 column, which is the auto incrementing primary key. The primary key is stored in tblBase in column bar_id, which will identify any Base as being a Bar.

Now, for the real kicker: How does one insert a new record into tblBar, when the only column is the auto incrementing primary key? I suppose I could do

insert into tblBar(bar_id) 
select max(bar_id) + 1 
from tblBar

but that seems a bit....clunky.

1 Answer 1


Doing a insert .. select max() ... is a bad idea to begin with as it doesn't work correctly in a multi user environment. Plus it would get the sequence behind the serial column out of sync with the already generated values.

What you are looking for is:

insert into bar (bar_id) values (default);

Another option would be explicitly call nextval() when inserting.

insert into bar (bar_id) values (nextval('bar_bar_id_seq'));
  • 1
    That did it for me. I also discovered that you can do the same with Linq2DB in C# with bar b = new bar; db.Insert(b). Thanks!
    – CurtisHx
    Oct 8, 2014 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.