带有MSSQL Doctrine的Symfony 2.6无法更新数据库

时间:2017-12-01 09:07:00

标签: php sql-server symfony doctrine sqlsrv

嗨社区,

我遇到了Symfony 2和Doctrine与MSSQL数据库的问题。在" php app / console doctrine:database:create"和" php app / console doctrine:schema:create"一切都很好。但是在" php app / console doctrine:schema:update"在错误:

之后,ORM-Mapper或实体显示中没有任何更改
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'ALTER TABLE product DROP CONSTRAINT DF_D34A04AD_A5D6E63E':
SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]DF_D34A04AD_A5D6E63E is not a restriction.

请帮助

当我从名为" DF__product__timesta__38996AB5"的表中手动删除限制时,它出现了一个新的错误:

[Doctrine\DBAL\DBALException]
An exception occurred while executing 'ALTER TABLE product ALTER COLUMN timestamp DATETIME DEFAULT CURRENT_TIMESTAMP':
SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near the DEFAULT keyword.

请帮助

数据库

Microsoft SQL Server 2016

PHP的软件包:

php70.x86_64                        1.0-5.el7
php70-php.x86_64                    7.0.26-1.el7
php70-php-cli.x86_64                7.0.26-1.el7
php70-php-common.x86_64             7.0.26-1.el7
php70-php-json.x86_64               7.0.26-1.el7
php70-php-pdo.x86_64                7.0.26-1.el7
php70-php-sqlsrv.x86_64             4.3.0-1.el7
php70-php-xml.x86_64                7.0.26-1.el7
php70-runtime.x86_64                1.0-5.el7

parameters.yml

database_driver: sqlsrv

的src /的appbundle /实体/ Product.php     命名空间AppBundle \ Entity;

class Product
{
    private $name;
    private $price;
    private $description;
    private $timestamp;
}

的src /的appbundle /资源/配置/教义/ Product.orm.yml

AppBundle\Entity\Product:
    type: entity
    table: product
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
        price:
            type: decimal
            scale: 2
        description:
            type: test
        timestamp:
            type: datetime
            columnDefinition: DATETIME DEFAULT CURRENT_TIMESTAMP
            nullable: false

表格面板的导出结构。产品

CREATE TABLE IF NOT EXISTS "product" (
"id" INT(10,0) NOT NULL,
"name" NVARCHAR(100) NOT NULL,
"price" NUMERIC(10,2) NOT NULL,
"description" VARCHAR(max) NOT NULL,
"timestamp" DATETIME(3) NULL DEFAULT (getdate()),
PRIMARY KEY ("id")
);

作曲家秀-i

doctrine/annotations                 v1.2.6  Docblock Annotations Parser
doctrine/cache                       v1.4.1  Caching library offering an object-oriented API for many cache backends
doctrine/collections                 v1.3.0  Collections Abstraction library
doctrine/common                      v2.5.0  Common Library for Doctrine projects
doctrine/dbal                        v2.4.4  Database Abstraction Layer
doctrine/doctrine-bundle             v1.5.0  Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       v1.0.1  Symfony2 Bundle for Doctrine Cache
doctrine/inflector                   v1.0.1  Common String Manipulations with regard to casing and singular/plural rules.
doctrine/lexer                       v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                         v2.4.7  Object-Relational-Mapper for PHP
incenteev/composer-parameter-handler v2.1.1  Composer script handling your ignored parameter file
jdorn/sql-formatter                  v1.2.17 a PHP SQL highlighting library
kriswallsmith/assetic                v1.2.1  Asset Management for PHP
monolog/monolog                      1.15.0  Sends your logs to files, sockets, inboxes, databases and various web services
psr/log                              1.0.0   Common interface for logging libraries
sensio/distribution-bundle           v4.0.0  Base bundle for Symfony Distributions
sensio/framework-extra-bundle        v3.0.9  This bundle provides a way to configure your controllers with annotations
sensio/generator-bundle              v2.5.3  This bundle generates code for you
sensiolabs/security-checker          v2.0.5  A security checker for your composer.lock
swiftmailer/swiftmailer              v5.4.1  Swiftmailer, free feature-rich PHP mailer
symfony/assetic-bundle               v2.6.1  Integrates Assetic into Symfony2
symfony/monolog-bundle               v2.7.1  Symfony MonologBundle
symfony/swiftmailer-bundle           v2.3.8  Symfony SwiftmailerBundle
symfony/symfony                      v2.6.11 The Symfony PHP framework
twig/extensions                      v1.2.0  Common additional features for Twig that do not directly belong in core
twig/twig                            v1.18.2 Twig, the flexible, fast, and secure template language for PHP

1 个答案:

答案 0 :(得分:0)

这不是将默认值添加到列的有效语法:

ALTER TABLE product ALTER COLUMN timestamp DATETIME DEFAULT
CURRENT_TIMESTAMP

你应该使用

ALTER TABLE product ADD DEFAULT CURRENT_TIMESTAMP
 FOR [timestamp]

或者您可以使用约束名称:

ALTER TABLE product ADD CONSTRAINT DF_TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 FOR [timestamp]

您收到的第一个错误很奇怪,因为当您尝试删除不存在的约束时,您应该

  

'DF_D34A04AD_A5D6E63E'不是约束。

原因是您使用了错误的约束名称。它似乎不会自动生成默认约束名称,因为它使用了生成的表名+字段名。因此,请再次检查约束名称。