-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When load fixture with postgres, sequence numbers updated to wrong values/did not upgraded #8298
Comments
why would this occur with id=3 and not already with id=2?
this does also not match with the fixture data you supplied. Hard to verify. Please reopen with correct information |
It is long time closed project, I'm unable to verify. |
I have the same problem. Fixture for table <?php
return [
'push1' => [
'id' => 1,
'customer_id' => 1,
'type_id' => 1,
'data' => '{"likedById":2}',
'status' => 'new',
'errors' => 0,
'updated_at' => '2017-06-23 00:00:04',
],
'push2' => [
'id' => 2,
'customer_id' => 2,
'type_id' => 2,
'data' => '{"likedById":1}',
'status' => 'processing',
'errors' => 0,
'updated_at' => '2017-06-23 00:00:05',
],
'push3' => [
'id' => 3,
'customer_id' => 1,
'type_id' => 1,
'data' => '{"likedById":3}',
'status' => 'done',
'errors' => 0,
'updated_at' => '2017-06-23 00:00:02',
],
'push4' => [
'id' => 4,
'customer_id' => 5,
'type_id' => 1,
'data' => '{"likedById":4}',
'status' => 'not found',
'errors' => 1,
'updated_at' => '2017-06-23 00:00:07',
],
'push5' => [
'id' => 5,
'customer_id' => 4,
'type_id' => 2,
'data' => '{"likedById":2}',
'status' => 'failed',
'errors' => 1,
'updated_at' => '2017-06-23 00:00:06',
],
'push6' => [
'id' => 6,
'customer_id' => 4,
'type_id' => 2,
'data' => '{"likedById":1}',
'status' => 'new',
'errors' => 0,
'updated_at' => '2017-06-23 00:00:01',
],
'push7' => [
'id' => 7,
'customer_id' => 2,
'type_id' => 1,
'data' => '{"likedById":5}',
'status' => 'done',
'errors' => 0,
'updated_at' => '2017-06-23 00:00:00',
],
'push8' => [
'id' => 8,
'customer_id' => 1,
'type_id' => 2,
'data' => '{"likedById":4}',
'status' => 'failed',
'errors' => 20,
'updated_at' => '2017-06-23 00:00:03',
],
'push9' => [
'id' => 9,
'customer_id' => 2,
'type_id' => 2,
'data' => '{"likedById":1}',
'status' => 'processing',
'errors' => 15,
'updated_at' => '2017-06-23 00:00:09',
],
]; Then try to do INSERT INTO "push_notification" ("customer_id", "type_id", "data", "status") VALUES (1, 1, '[]', 'new') RETURNING "id"; The error will be: ERROR: duplicate key value violates unique constraint "push_notification_pkey"
DETAIL: Key (id)=(1) already exists. After sending the query 10 times the error disappears (when id becames |
I’m fixing it this way: protected function _before()
{
$this->tester->haveFixtures([
'notifications' => PushNotificationFixture::className(),
]);
// Postgres fix
Yii::$app->db
->createCommand("SELECT setval('push_notification_id_seq', max(id)) FROM push_notification;")
->execute();
} But it’s a kludge. |
What about something like this? Related method. Rough but working implementation. public function load()
{
$this->data = [];
$table = $this->getTableSchema();
foreach ($this->getData() as $alias => $row) {
$primaryKeys = $this->db->schema->insert($table->fullName, $row);
$this->data[$alias] = array_merge($row, $primaryKeys);
}
if ($table->sequenceName !== null && count($primaryKeys) > 0) {
//recalculate sequence based on last inserted id
$primaryKey = key($primaryKeys);
$lastId = (new Query())->select('max('. $primaryKey .')')->from($table->fullName)->scalar();
$this->db->createCommand()->resetSequence($table->fullName, $lastId)->execute();
}
} |
@StalkAlex if you have time please do a pull request. |
… unMerge method the unMerge method will remove an author from the graph by removing all the outward edges from itself. I also stumbled into the following errors when writing tests that create or delete an AuthorRel object in database: CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "author_rel_pkey" DETAIL: Key (id)=(1) already exists. This is because the data fixtures for author_rel are loaded before the tests are run and for some reason either CDbTestCase or CdbFixtureManager set db connection are unable to auto-increment from the last id used in the fixture. I found a workaround from this issue (which is for a different context/framework version but same problem): yiisoft/yii2#8298 Didn't put it in a setUp function as it makes the other tests fail.
… unMerge method the unMerge method will remove an author from the graph by removing all the outward edges from itself. I also stumbled into the following errors when writing tests that create or delete an AuthorRel object in database: CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "author_rel_pkey" DETAIL: Key (id)=(1) already exists. This is because the data fixtures for author_rel are loaded before the tests are run and for some reason either CDbTestCase or CdbFixtureManager set db connection are unable to auto-increment from the last id used in the fixture. I found a workaround from this issue (which is for a different context/framework version but same problem): yiisoft/yii2#8298 Didn't put it in a setUp function as it makes the other tests fail.
…updated to wrong values/did not upgraded update sequence after load data
…updated to wrong values/did not upgraded update sequence after load data
When I load fixtures, sequence number of tables are not updated or updated to wrong values,
I'm using this fix:
Error message:
Fixture data:
The text was updated successfully, but these errors were encountered: