cakephp 2.x saveAll with 2 primaryKey - cakephp-2.0

My table has 2 column: cid AND code
Both of them are primary keys, cid is primary key 1, code is primary key 2
I am using cakephp and i'm trying to save the data with:
cid [1, 2, 1, 2]
code [11, 11, 12, 12]
But i receive error message:
Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'
Please help me, many thanks.

This is not a php error, what happened here is that you caused a mysql database error and the cake error class is returning that to you. On saveAll you are attempting an insert into one of your tables that already has a particular primary key in it. Run these queries to check:
SELECT * FROM your_table WHERE cid = 1;
SELECT * FROM your_table WHERE code = 1;
If anything comes up that is the offending record. Delete it and try again. Also you should not be force-ably setting the primary keys of your tables you should use autoincrement instead. And I know from experience that cake gives you a hard time with tables that have multiple primary keys. I hope that helps.

Related

Insert row with same key after delete throws key constraint error

I have a situation where we need to change an employee record to have a different ID. I've deleted the record that caused this, and intend to move the still intact record to the new ID.
However when I attempt to do the insert, I'm getting a key constraint error.
Msg 2627, Level 14, State 1, Line 1
Violation of PRIMARY KEY constraint 'PK_Employee'. Cannot insert duplicate key in object 'dbo.Employee'. The duplicate key value is (2682).
Upon reading, I understand that I'd need to rebuild indexes to accomplish this. I've done this for the non clustered indexes on the table. Does this also need to be done for the clustered indexes?
Edited to include SQL query and insert:
With that SELECT statement you're using to select the rows to be inserted (without any WHERE clause), you're inserting ALL the existing employees back into the table again.
While the first INSERT with ID = 2682 will work (since the entry with ID = 2682 has been removed before that), once SQL Server tries to insert the second employee fetched in the SELECT, also with ID = 2682, it will **fail ** (since there's already a new entry with that ID) ...

SQL Server Automatic Index Changes (A filter on my index is gone)

I'm using SQL Server 2008
I know that an index on a view will be deleted without prejudice nor warning if you change the view definition. I am wondering if there are any other known similar automatic changes to indexes and if so where can I read about them?
My situation:
I had a unique non-clustered filtered index on a table.
The index key columns are the same as the primary key of the table.
Only one other column is included.
Today I when things were going slowly, I realized that the filter is now gone.
One of the primary key column types was recently changed to a different type. So I'm guessing that may have been when the filter removal happened, but I can't find verification of that anywhere.
If it helps, I could not simply add the filter back because when trying to recreate, 'The new index definition does not match the constraint being enforced by the existing index.'
Neither can I simply drop the index to recreate it because apparently SQL Server decided to use the index to enforce foreign key constraints instead of the clustered PK index. I will have to delete foreign key constraints before I do.
All of our devs said that they didn't mess with the index. One of us may be forgetful. Or lying. Or someone may have changed something and not have read very carefully the warning messages. I don't know if this is something that could be automatic so I'm hoping you guys can inform me.
Thanks!
I guess someone is fishing :)
Here's test code which shows that you can't change PK's column data type without dropping indexes off first:
create table dbo.worker
(
id int identity not null constraint PK_worker primary key,
name varchar(100) not null
);
-- Filtered index based in PK's primary index
create nonclustered index ix_worker
on dbo.worker (id)
where id < 3;
-- Try to change PK's column data type...
alter table dbo.worker
alter column id bigint;
SQL Server generates following errors:
Msg 5074, Level 16, State 1,
Line 8 The index 'ix_worker' is dependent on column 'id'.
Msg 5074, Level 16, State 1, Line 8
The object 'PK_worker' is dependent on column 'id'.
Msg 5074, Level 16, State 1, Line 8
The index 'ix_worker' is dependent on column 'id'.
Msg 4922, Level 16, State 9, Line 8
ALTER TABLE ALTER COLUMN id failed because one or more objects access this column.

Some primary keys are missing in the SQL Server table. Why is that so?? is the any transaction which is not ended or something else?

There is data saving part in my work. While doing it data has saved in the table, but as my concern primary key should be last primary + 1, but some primary keys are not there. Why is that so??
My my table has primary keys 1,2,3,4,5,6,7,8,9,10,12
In this primary key 11 is missing.
When a record, in your case 11 is deleted and another record is added to the table it is added as 12 and not 11.
Identity and Sequence column don't guaranty end-to-end numbering +1

Postgres INSERT INTO... SELECT violates foreign key constraint

I'm having a really, really strange issue with postgres. I'm trying to generate GUIDs for business objects in my database, and I'm using a new schema for this. I've done this with several business objects already; the code I'm using here has been tested and has worked in other scenarios.
Here's the definition for the new table:
CREATE TABLE guid.public_obj
(
guid uuid NOT NULL DEFAULT uuid_generate_v4(),
id integer NOT NULL,
CONSTRAINT obj_guid_pkey PRIMARY KEY (guid),
CONSTRAINT obj_id_fkey FOREIGN KEY (id)
REFERENCES obj (obj_id)
ON UPDATE CASCADE ON DELETE CASCADE
)
However when I try to backfill this using the following code, I get a SQL state 23503 claiming that I'm violating the foreign key constraint.
INSERT INTO guid.public_obj (guid, id)
SELECT uuid_generate_v4(), o.obj_id
FROM obj o;
ERROR: insert or update on table "public_obj" violates foreign key constraint "obj_id_fkey"
SQL state: 23503
Detail: Key (id)=(-2) is not present in table "obj".
However, if I do a SELECT on the source table, the value is definitely present:
SELECT uuid_generate_v4(), o.obj_id
FROM obj o
WHERE obj_id = -2;
"0f218286-5b55-4836-8d70-54cfb117d836";-2
I'm baffled as to why postgres might think I'm violating the fkey constraint when I'm pulling the value directly out of the corresponding table. The only constraint on obj_id in the source table definition is that it's the primary key. It's defined as a serial; the select returns it as an integer. Please help!
Okay, apparently the reason this is failing is because unbeknownst to me the table (which, I stress, does not contain many elements) is partitioned. If I do a SELECT COUNT(*) FROM obj; it returns 348, but if I do a SELECT COUNT(*) FROM ONLY obj; it returns 44. Thus, there are two problems: first, some of the data in the table has not been partitioned correctly (there exists unpartitioned data in the parent table), and second, the data I'm interested in is split out across multiple child tables and the fkey constraint on the parent table fails because the data isn't actually in the parent table. (As a note, this is not my architecture; I'm having to work with something that's been around for quite some time.)
The partitioning is by implicit type (there are three partitions, each of which contains rows relating to a specific subtype of obj) and I think the eventual solution is going to be creating GUID tables for each of the subtypes. I'm going to have to handle the stuff that's actually in the obj table probably by selecting it into a temp table, dropping the rows from the obj table, then reinserting them so that they can be partitioned properly.

Oracle sequence primary key violated with Doctrine and Symfony

I have a table in Oracle which has some values with different primary keys, for example values 1, 2, 27, 125...
In my entity in Symfony the annotation is:
* #ORM\Column(name="MY_KEY", type="decimal")
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="TABLE_MY_KEY_seq", allocationSize=1, initialValue=1)
And the Sequence generated in Oracle is:
When I insert a new register in that table, doctrine tries to insert in the first position (id = 1) so I have a error (PK violated, primary key duplicated), then tries with the second position (same error...) until finds I empty position. N times after we have the same error because tries to insert Primary Key with value 27 o 125 which is already in database. ¿How can I control this? Because from the first time, I need to have these values in the database and I don't know what values will be.
Thanks :)
You should alter the sequence. Suppose your max id in the table is 1500, then run
ALTER SEQUENCE TABLE_MY_KEY_seq
MAXVALUE 1500;
Your first id will be 1501.
Otherwise you should implement your own custom strategy as described here
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies

Resources