-
Notifications
You must be signed in to change notification settings - Fork 354
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
Provide createdAt/updatedAt fields #1652
Comments
This would have to be stored in the database, and I don't believe that drift should add those columns by default. You can easily add those fields to your table with a mixin: mixin WithModificationTimestamps on Table {
DateTimeColumn get createdAt => dateTime().withDefault(currentDateAndTime)();
DateTimeColumn get updatedAt => dateTime().withDefault(currentDateAndTime)();
} Then, you just have to use MigrationStrategy get migration {
return MigrationStrategy(onCreate: (m) async {
await m.createAll();
for (final table in allTables) {
if (table is WithModifiedTimestamps) {
final name = table.entityName;
await customStatement('''
CREATE TRIGGER updated_at_$name AFTER UPDATE ON $name
BEGIN
UPDATE $name SET updated_at = CURRENT_TIMESTAMP;
END;
''');
}
}
});
} |
Following up on this: Especially when using @override
List<DatabaseSchemaEntity> get allSchemaEntities {
final result = super.allSchemaEntities;
for (final table in result.whereType<TableInfo>().toList()) {
if (table is WithModifiedTimestamps) {
final name = table.entityName;
final triggerName = 'updated_at_$name';
result.add(
Trigger(
'''
CREATE TRIGGER $triggerName AFTER UPDATE ON $name BEGIN
UPDATE $name SET updated_at = CURRENT_TIMESTAMP;
END;
''',
triggerName,
),
);
}
}
return result;
} With this override, the custom |
Why this very good explain is not in your documentation ? Thank you for your advice. I confirm that It works like a charm in my part. |
Provide createdAt/updatedAt fields.
Automatically write createdAt when a row is created,
and update updatedAt when a row is updated.
The text was updated successfully, but these errors were encountered: