Welcome Guest, Not a member yet? Register   Sign In
Active Record ,Insert Auto Increment Problem?
#1

[eluser]Angkor[/eluser]
I have a talbe:
Code:
CREATE TABLE company
(
  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)
)
WITH (OIDS=FALSE);
ALTER TABLE company OWNER TO postgres;


My Controller

Code:
function add(){

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

        $this->load->library('session');



        //Also we are going to need the sites model

        $this->load->model('company_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:

Code:
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
  //accordingly.
  $result = 0;
  //Check if $data is not empty
  if(!empty($data))
  {
    //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:

Code:
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?

Thanks
#2

[eluser]WanWizard[/eluser]
Make sure the zip_code you want to insert is unique? Or remove the constraint?
#3

[eluser]Angkor[/eluser]
thanks.for quick replied ,it's my mistake.
#4

[eluser]mddd[/eluser]
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).




Theme © iAndrew 2016 - Forum software by © MyBB