根据学说进行迁移时出错
Error when making a migration on doctrine
我正在尝试通过为我刚刚创建的实体进行迁移来将 table 创建到 sql 服务器中,但它在执行查询时抛出异常:
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,我创建了一个新的用户实体
我进行了迁移:
$ 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 说的做了,它成功了。
有人可以解释一下为什么当我使用 maker-bundle 进行迁移时,查询如下:
$this->addSql('EXEC sp_addextendedproperty N\'MS_Description\', N\'(DC2Type:json)\', N\'SCHEMA\', \'dbo\', N\'TABLE\', \'[user]\', N\'COLUMN\', roles');
没有显示 user 而不是 [user]?
最终解决方案
好像 'user' 是一个保留字,所以我将 class 重构为一个不同的名称,它从头到尾都工作得很好
您的代码应如下所示:
EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:json)', N'SCHEMA', 'dbo', N'TABLE', 'user', N'COLUMN', 'roles'
错误告诉您对象 'dbo.[user].roles' 不存在,实际上您的对象 'dbo.user.roles' 没有括号,因此您应该使用 'user'
作为 [=23] 的参数=],不是 '[user]'
问题是 'user' 是 sql 服务器中的保留字,所以我将 class 重构为不同的名称,它从头到尾都运行良好
我正在尝试通过为我刚刚创建的实体进行迁移来将 table 创建到 sql 服务器中,但它在执行查询时抛出异常:
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,我创建了一个新的用户实体
我进行了迁移:
$ 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 说的做了,它成功了。
有人可以解释一下为什么当我使用 maker-bundle 进行迁移时,查询如下:
$this->addSql('EXEC sp_addextendedproperty N\'MS_Description\', N\'(DC2Type:json)\', N\'SCHEMA\', \'dbo\', N\'TABLE\', \'[user]\', N\'COLUMN\', roles');
没有显示 user 而不是 [user]?
最终解决方案 好像 'user' 是一个保留字,所以我将 class 重构为一个不同的名称,它从头到尾都工作得很好
您的代码应如下所示:
EXEC sp_addextendedproperty N'MS_Description', N'(DC2Type:json)', N'SCHEMA', 'dbo', N'TABLE', 'user', N'COLUMN', 'roles'
错误告诉您对象 'dbo.[user].roles' 不存在,实际上您的对象 'dbo.user.roles' 没有括号,因此您应该使用 'user'
作为 [=23] 的参数=],不是 '[user]'
问题是 'user' 是 sql 服务器中的保留字,所以我将 class 重构为不同的名称,它从头到尾都运行良好