'How do I make doctrine support timestamp columns?
I'm trying to apply the following migration:
Schema::table('users', function (Blueprint $table) {
$table->timestamp('created_at')->useCurrent()->change();
});
But artisan says:
[Doctrine\DBAL\DBALException]
Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL
\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). I
f this error occurs during database introspection then you might have forgot to register all database types for a
Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMapp
edDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping inform
ation.
When I try to install mmerian/doctrine-timestamp (composer install mmerian/doctrine-timestamp), composer says:
[InvalidArgumentException]
Could not find package mmerian/doctrine-timestamp at any version for your minimum-stability (stable). Check the pa
ckage spelling or your minimum-stability
What do I do?
UPD With composer require mmerian/doctrine-timestamp=dev-master, I was able to install the package, then added Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp'); before Schema::table statement, but now I've got the other error:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: ALTER TABLE u
sers CHANGE created_at created_at INT DEFAULT 'CURRENT_TIMESTAMP' NOT NULL)
UPD I checked again if it works with mmerian/doctrine-timestamp, since I added only first of the lines from the docs back then (or the doc was updated):
Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');
DB::getDoctrineConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('Timestamp', 'timestamp');
But it doesn't help as well. The migration succeeds, but the column definition doesn't change.
Solution 1:[1]
hi~ you can use "datetime" type:
Schema::table('orders', function ($table) {
$table->datetime('pay_time')->nullable()->change();
});
Solution 2:[2]
if you want to make migration for current timestamp and get the error "Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType()" then use like this
\DB::statement("ALTER TABLE `order_status_logs` CHANGE `created_at` `created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
Solution 3:[3]
Set minimum-stability setting to dev in your composer.json, because mmerian/doctrine-timestamp has only dev-master version, to example:
{
"minimum-stability": "dev",
"require": {
...
}
}
Then, when bootstraping your doctrine connection:
Type::addType('timestamp', 'DoctrineTimestamp\DBAL\Types\Timestamp');
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('Timestamp', 'timestamp');
Solution 4:[4]
I builded this for it, since Doctrine does not want to support it cause it's a MySQL-specific column type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? |
| Solution 2 | Anjani Barnwal |
| Solution 3 | maximkou |
| Solution 4 | Mark Topper |
