在学说上进行迁移时出错 [英] Error when making a migration on doctrine

查看:92
本文介绍了在学说上进行迁移时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过对刚创建的实体进行迁移来在sql服务器中创建表,但是在执行查询时会抛出异常:

I'm trying to create a table into a sql server by making a migration for the entity I just created, but it throws an exception when executing the query:

SQLSTATE [42000, 15135]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Object is invalid. Extended properties are not permitted on 'dbo.[user].roles', or the object does not exist.

通过使用maker-bundle,我创建了一个新的User实体

By using maker-bundle I created a new User entity

我进行了迁移:

$ php bin /控制台make:migration

$ php bin/console make:migration

  Success!


 Next: Review the new migration "src/Migrations/Version20181121121145.php"
 Then: Run the migration with php bin/console doctrine:migrations:migrate
 See https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

然后迁移迁移时:

$ php bin/console doctrine:migrations:migrate

                    Application Migrations


WARNING! You are about to execute a database migration that could result in schema changes and data loss. Are you sure you wish to continue? (y/n)y
Migrating up to 20181121121145 from 0

  ++ migrating 20181121121145

     -> CREATE TABLE [user] (id INT IDENTITY NOT NULL, username NVARCHAR(180) NOT NULL, roles VARCHAR(MAX) NOT NULL, password NVARCHAR(255) NOT NULL, email NVARCHAR(255) NOT NULL, PRIMARY KEY (id))
     -> CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON [user] (username) WHERE username IS NOT NULL
     -> EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:json)', N'SCHEMA', 'dbo', N'TABLE', '[user]', N'COLUMN', roles
Migration 20181121121145 failed during Execution. Error An exception occurred while executing 'EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:json)', N'SCHEMA', 'dbo', N'TABLE', '[user]', N'COLUMN', roles':

SQLSTATE [42000, 15135]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Object is invalid. Extended properties are not permitted on 'dbo.[user].roles', or the object does not exist.

In DBALException.php line 187:

  An exception occurred while executing 'EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:json)', N'SCHEMA', '
  dbo', N'TABLE', '[user]', N'COLUMN', roles':

  SQLSTATE [42000, 15135]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Object is invalid. Extended properties
   are not permitted on 'dbo.[user].roles', or the object does not exist.


In SQLSrvException.php line 57:

  SQLSTATE [42000, 15135]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Object is invalid. Extended properties
   are not permitted on 'dbo.[user].roles', or the object does not exist.


doctrine:migrations:migrate [--write-sql [WRITE-SQL]] [--dry-run] [--query-time] [--allow-no-migration] [--configuration [CONFIGURATION]] [--db-configuration [DB-CONFIGURATION]] [--db DB] [--em EM] [--shard SHARD] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> [<version>]

迁移文件如下:

//Version20181121121145.php
<?php declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20181121121145 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mssql', 'Migration can only be executed safely on \'mssql\'.');

        $this->addSql('CREATE TABLE [user] (id INT IDENTITY NOT NULL, username NVARCHAR(180) NOT NULL, roles VARCHAR(MAX) NOT NULL, password NVARCHAR(255) NOT NULL, email NVARCHAR(255) NOT NULL, PRIMARY KEY (id))');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649F85E0677 ON [user] (username) WHERE username IS NOT NULL');
        $this->addSql('EXEC sp_addextendedproperty N\'MS_Description\', N\'(DC2Type:json)\', N\'SCHEMA\', \'dbo\', N\'TABLE\', \'[user]\', N\'COLUMN\', roles');
    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mssql', 'Migration can only be executed safely on \'mssql\'.');

        $this->addSql('CREATE SCHEMA db_accessadmin');
        $this->addSql('CREATE SCHEMA db_backupoperator');
        $this->addSql('CREATE SCHEMA db_datareader');
        $this->addSql('CREATE SCHEMA db_datawriter');
        $this->addSql('CREATE SCHEMA db_ddladmin');
        $this->addSql('CREATE SCHEMA db_denydatareader');
        $this->addSql('CREATE SCHEMA db_denydatawriter');
        $this->addSql('CREATE SCHEMA db_owner');
        $this->addSql('CREATE SCHEMA db_securityadmin');
        $this->addSql('CREATE SCHEMA dbo');
        $this->addSql('DROP TABLE [user]');
    }
}

谢谢。

第一个解决方案:

我打开了SQL Server Management Studio,并逐个测试了三个查询。可以找到前两个,但是第三个失败,所以我按照@sepupic的说明进行了工作。

I opened SQL Server Management Studio and tested one by one the three queries. The first two worked just find but the third one failed, so I did what @sepupic said and it worked.

有人可以解释一下为什么我使用maker-bundle,查询如下:

Can somebody explain me why when I made the migration using the maker-bundle, the query below:

$this->addSql('EXEC sp_addextendedproperty N\'MS_Description\', N\'(DC2Type:json)\', N\'SCHEMA\', \'dbo\', N\'TABLE\', \'[user]\', N\'COLUMN\', roles');

不显示 user 而不是 [user]

最终解决方案
似乎用户是保留字,因此我将类重构为一个不同的名称,并且从头到尾都很好用

Final Solution It seems to be 'user' is a reserved word, so I refactored the class to a different name and it worked just fine from the beginning to the end

推荐答案

问题是用户是保留字在sql server中,所以我将类重构为其他名称,并且从头到尾都很好用

The problem is that 'user' is a reserved word in sql server, so I refactored the class to a different name and it worked just fine from the beginning to the end

这篇关于在学说上进行迁移时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆