1
votes

Hey Community,

i have a Problem with Symfony 2 and Doctrine with a MSSQL-Database. On "php app/console doctrine:database:create" and "php app/console doctrine:schema:create" all fine. But on "php app/console doctrine:schema:update" without any changes in ORM-Mapper or Entity display following Error:

[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.

PLEASE HELP

When i delete the restriction manually from the table named "DF__product__timesta__38996AB5", its a new Error:

[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.

PLEASE HELP

Database

Microsoft SQL Server 2016

Packages for 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/Entity/Product.php namespace AppBundle\Entity;

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

src/AppBundle/Resources/config/doctrine/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

Export structure of table panel.product

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")
);

composer show -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

1 Answers

0
votes

This is not a valid syntax for adding default to a column:

ALTER TABLE product ALTER COLUMN timestamp DATETIME DEFAULT
CURRENT_TIMESTAMP

You should use

ALTER TABLE product ADD DEFAULT CURRENT_TIMESTAMP
 FOR [timestamp]

Or you can use a name for your constraint:

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

The first error that you receive is strange because when you try to drop non-existing constraint you should get

'DF_D34A04AD_A5D6E63E' is not a constraint.

The cause is you use wrong constraint name. It does not seem automatically generated default constraint name as when it's generated table name + field name are used. So check the constraint name once again.