• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active Record ,Insert Auto Increment Problem?

I have a talbe:
  company_id serial NOT NULL,
  company_name character varying(80) NOT NULL,
  description character varying(255),
  address character varying(100) NOT NULL,
  city character varying(30),
  zip_code character(5),
  country character varying(15),
  website character varying(50),
  phone1 character varying(16) NOT NULL,
  phone2 character varying(16),
  fax character varying(16),
  CONSTRAINT company_pk PRIMARY KEY (company_id),
  CONSTRAINT company_company_name_key UNIQUE (company_name),
  CONSTRAINT company_fax_key UNIQUE (fax),
  CONSTRAINT company_phone1_key UNIQUE (phone1),
  CONSTRAINT company_website_key UNIQUE (website),
  CONSTRAINT company_zip_code_key UNIQUE (zip_code)
ALTER TABLE company OWNER TO postgres;

My Controller

function add(){

        //we are going to use the session library to create some flash data


        //Also we are going to need the sites model



        //Put the received data into an array.

        $data = array(
                       'company_name' => $_POST['company_name'],

                       'description' => $_POST['description'],

                       'address' => $_POST['address'],

                       'city' => $_POST['city'],

                       'zip_code' => $_POST['zip_code'],

                       'country' => $_POST['country'],

                       'website' => $_POST['website'],

                       'phone1' => $_POST['phone1'],

                       'phone2' => $_POST['phone2'],

                       'fax' => $_POST['fax']



    //We call the model add_site function, passing the created array as a parameter, we save returned data into the $sites variable.            

        $companies = $this->company_model->add_company($data);

My Model:

function add_company($data)
  //We create $result variable with a default value of 0, so if no
  //insert is done, $result will be returned with 0 value, and we
  //will be able, in the controller, to create the flash data
  $result = 0;
  //Check if $data is not empty
    //insert $data with the insert method

      $result = $this->db->insert('company', $data);
  //return the value
  return $result;

I got an error when I do the insert:

A Database Error Occurred

Error Number:

ERROR: duplicate key value violates unique constraint "company_zip_code_key"

INSERT INTO "company" ("company_name", "description", "address", "city", "zip_code", "country", "website", "phone1", "phone2", "fax") VALUES ('ANANA', 'HARDWARE', 'Phnom Penh', 'Phnom Penh', '855', 'cambodia', 'ananacomputer.com', '012 583 654 ', '013 258 368', '025 553 5556')

Anybody Know how to fix it?


Make sure the zip_code you want to insert is unique? Or remove the constraint?

thanks.for quick replied ,it's my mistake.

You have defined your table in such a way that no two records can have the same id, name, fax, phone, website or zipcode.
Now you are trying to insert something with the same zip code as an existing record.

Change your table definition (indexes!) to remove this 'unique key' requirement on fields like zipcode (and I would say also on phone, fax
as they might be shared by multiple companies? This depends on your business I guess).

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.