【问题标题】:Migration error on timestamp change field时间戳更改字段上的迁移错误
【发布时间】:2019-01-18 14:59:51
【问题描述】:

在 laravel 5.7/mysql 5 应用程序中,我想将默认值设置为时间戳字段(在创建时未设置):

   $table->timestamp('created_at')->useCurrent()->change();

但我得到了错误:

 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(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

  at /mnt/_work_sdb8/wwwroot/lar/BoxBooking2/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:267
    263|      * @return \Doctrine\DBAL\DBALException
    264|      */
    265|     public static function unknownColumnType($name)
    266|     {                                                                                                                                                                                                         
  > 267|         return new self('Unknown column type "' . $name . '" requested. Any Doctrine type that you use has ' .                                                                                                
    268|             'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' .                                                                                                    
    269|             'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' .                                                                                              
    270|             'introspection then you might have forgotten to register all database types for a Doctrine Type. Use ' .                                                                                          
    271|             'AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' .                                                                                                           

通常我对时间戳字段没有任何问题,我认为方法 ->change 引发了这个问题。 如何解决?

修改区块#1: 我已经安装了 我的 composer.json :

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.1.3",
        "doctrine/dbal": "^2.9",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.7.*",
        "laravel/tinker": "^1.0",
        "marktopper/doctrine-dbal-timestamp-type": "^1.0",
        "mews/purifier": "^2.1",
        "proengsoft/laravel-jsvalidation": ">2.2.0",
        "yajra/laravel-datatables-oracle": "~8.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^2.0",
        "phpunit/phpunit": "^7.0"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

谷歌搜索我发现我还需要安装 marktopper/doctrine-dbal-timestamp-type https://github.com/art-institute-of-chicago/data-aggregator/commit/581f4f318468681c71c62103280daedc67bbe118 我安装了它,运行迁移没有错误,但 created_at 默认时间戳未定义,因此添加的新行在 created_at 中为空。

$ php artisan --version
Laravel Framework 5.7.21
$ node -v 
v10.15.0
$ npm -v 
6.5.0

谢谢!

【问题讨论】:

  • 你添加了学说依赖吗?使用change()时需要安装这个;作曲家需要学说/dbal
  • 请看 MODIFIED BLOCK #1

标签: laravel-5 migration


【解决方案1】:

documentation 声明您无法更改/修改时区列类型。

只有以下列类型可以“更改”:bigInteger、binary、boolean、date、dateTime、dateTimeTz、decimal、integer、json、longText、mediumText、smallInteger、string、text、time、unsignedBigInteger、unsignedInteger 和 unsignedSmallInteger。

最好的办法是使用原始 sql 查询来更新列,例如:

DB::statement('ALTER TABLE `table_name` CHANGE `column_name` `column_name` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;');

【讨论】:

    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多