Use next_sequence_id to create a new child of the dataset, with a unique id

def new_unique_object(self, table_class, sequence_id=None, force_query=False, **kwargs):
        """Use next_sequence_id to create a new child of the dataset, with a unique id"""
        from sqlalchemy.exc import IntegrityError
        from sqlalchemy.orm.exc import FlushError

        # If a sequence ID was specified, the caller is certain
        #  that there is no potential for conflicts,
        # so there is no need to commit here.
        if not sequence_id:
            commit = True
            sequence_id = self.next_sequence_id(table_class, force_query=force_query)
        else:
            commit = False

        o = table_class(
            d_vid=self.vid,
            **kwargs
        )

        o.update_id(sequence_id)

        if commit is False:
            return o

        self.commit()

        if self._database.driver == 'sqlite':
            # The Sqlite database can't have concurrency, so there no problem.
            self.session.add(o)
            self.commit()
            return o
        else: # Postgres. Concurrency is a bitch.
            table_name = table_class.__tablename__
            child_sequence_id = table_class.sequence_id.property.columns[0].name

        try:

            self.session.add(o)
            self.commit()
            return o

        except (IntegrityError, FlushError) as e:
            self.rollback()
            self.session.merge(self)
            print 'Failed'
            return None

        return
        # This is horrible, but it's the only thing that has worked for both
        # Sqlite and Postgres in both single processes and multiprocesses.
        d_vid = self.vid
        while True:
            try:
                self.session.add(o)
                self.commit()
                return o

            except (IntegrityError, FlushError) as e:

                self.rollback()

                self.session.expunge_all()
                ds = self._database.dataset(d_vid)
                sequence_id = ds.next_sequence_id(table_class, force_query=True)

                o.update_id(sequence_id)

            except Exception as e:

                print('Completely failed to get a new {} sequence_id; {}'.format(table_class, e))
                self.rollback()
                import traceback

                # This bit is helpful in a multiprocessing run.
                tb = traceback.format_exc()

                print(tb)
                raise