Skip to content

Commit 8102c7a

Browse files
committed
fix(sql): fix default created / last edited default values for m2m tables
The junction table SummitEventType_SummitTicketType was created (via CreateTableTrait) with Created and LastEdited columns that are NOT NULL but have no default value. When Doctrine's ManyToManyPersister inserts into this table (e.g., when updating allowed_ticket_types on an event type), it only inserts the FK columns (SummitEventTypeID, SummitTicketTypeID). MySQL rejects the insert because Created has no value and no default.
1 parent 8eeb0e0 commit 8102c7a

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

database/migrations/model/CreateTableTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public static function createTable(Schema $schema, string $table_name, callable
3131
$table->primary("ID");
3232
$table->string('ClassName')->setDefault($table_name);
3333
$table->index("ClassName", "ClassName");
34-
$table->timestamp('Created');
35-
$table->timestamp('LastEdited');
34+
$table->timestamp('Created')->setDefault('CURRENT_TIMESTAMP');
35+
$table->timestamp('LastEdited')->setDefault('CURRENT_TIMESTAMP');
3636
if($add_columns_callback)
3737
$add_columns_callback($table);
3838
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php namespace Database\Migrations\Model;
2+
/**
3+
* Copyright 2026 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use Doctrine\Migrations\AbstractMigration;
16+
use Doctrine\DBAL\Schema\Schema as Schema;
17+
18+
/**
19+
* Class Version20260313144000
20+
* @package Database\Migrations\Model
21+
*
22+
* Adds DEFAULT CURRENT_TIMESTAMP to Created and LastEdited columns on
23+
* ManyToMany junction tables so Doctrine's ManyToManyPersister can insert
24+
* rows without explicitly providing these values.
25+
*/
26+
final class Version20260313144000 extends AbstractMigration
27+
{
28+
/**
29+
* @param Schema $schema
30+
*/
31+
public function up(Schema $schema): void
32+
{
33+
$tables = [
34+
'SummitEventType_SummitTicketType',
35+
'SummitEvent_SummitTicketType',
36+
];
37+
38+
foreach ($tables as $table) {
39+
$this->addSql("ALTER TABLE {$table} MODIFY Created datetime NOT NULL DEFAULT CURRENT_TIMESTAMP");
40+
$this->addSql("ALTER TABLE {$table} MODIFY LastEdited datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
41+
}
42+
}
43+
44+
/**
45+
* @param Schema $schema
46+
*/
47+
public function down(Schema $schema): void
48+
{
49+
$tables = [
50+
'SummitEventType_SummitTicketType',
51+
'SummitEvent_SummitTicketType',
52+
];
53+
54+
foreach ($tables as $table) {
55+
$this->addSql("ALTER TABLE {$table} MODIFY Created datetime NOT NULL");
56+
$this->addSql("ALTER TABLE {$table} MODIFY LastEdited datetime NOT NULL");
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)