[odb-users] odb-2.2.1: calling db->persist() twice on the the instance results in two database entries

Hugo.Mildenberger at web.de Hugo.Mildenberger at web.de
Sat Mar 2 05:21:50 EST 2013


ODB creates two copies of the same instance in the database if db->persist() was called twice on it. I looked up the documentation but did not found this being documented behaviour. Perhaps I missed something important here? I had expected db->persist() throwing an exception in this case. 

Specifically, this program:

 int main (int argc, char* argv[]) {

     cif::imp::telecom telecom("private","0","email","sombody.1 at example.com");
     try {
          std::auto_ptr<odb::database> db (new odb::pgsql::database (argc, argv));
          odb::transaction t (db->begin ());
          db->persist(telecom);
          telecom.set_type("voip");
          telecom.set_uri("somebody.2 at example.com");
          db->persist(telecom);
          t.commit ();
     } catch (const odb::exception& e) {
        std::cerr << e.what () << std::endl;
        return 1;
     }
     return 0;
}

and this class definition:


  namespace odb {
  class access; 
  }

  namespace cif {
  namespace imp {


  #pragma db object
  class telecom {
	public:

	    [...]

	private:
		friend class odb::access;
		
		std::string _type;

		std::string _priority;

		std::string _uri;

		std::string _purpose;

		#pragma   db id auto
		std::size_t _id;
   };

   } /* namespace cif */
   } /* namespace imp */


and this (PostgreSQL) table definition:

  /* This file was generated by ODB, object-relational mapping (ORM)
   * compiler for C++.
   */

  DROP TABLE IF EXISTS "telecom" CASCADE;
  DROP VIEW IF EXISTS "telecom" CASCADE;

  CREATE TABLE "telecom" (
    "type" TEXT NOT NULL,
    "priority" TEXT NOT NULL,
    "uri" TEXT NOT NULL,
    "purpose" TEXT NOT NULL,
    "id" BIGSERIAL NOT NULL PRIMARY KEY);

                                                                                                                             
resulted in:

  $ psql -c "select * from telecom"
    type  | priority |          uri           | purpose | id 
   -------+----------+------------------------+---------+----
    email | 0        | sombody.1 at example.com  | private |  1
    voip  | 0        | somebody.2 at example.com | private |  2
                                            



More information about the odb-users mailing list