postgresql GeneratedValue是在本地开发环境中生成的,而不是在www.example.com(Symfony 6.1)上的生产环境中platform.sh的

8yparm6h  于 2023-05-17  发布在  PostgreSQL
关注(0)|答案(1)|浏览(99)

值:

#[ORM\Entity(repositoryClass: CompanyRepository::class)]
class Company
{
  #[ORM\Id]
  #[ORM\GeneratedValue]
  #[ORM\Column]
  private ?int $id = null;

代码:

dump($company);
dump($entityManager->getUnitOfWork()->getScheduledEntityInsertions() );

$entityManager->persist($company);
dump($company);
dump($entityManager->getUnitOfWork()->getScheduledEntityInsertions() );

$entityManager->persist($user);
dump($entityManager->getUnitOfWork()->getScheduledEntityInsertions() );
die();

$entityManager->flush();

本地开发环境中的输出:

RegistrationController.php on line 65:
App\Entity\Company {#1258 ▼
  -id: null
  -comment: null
  -employees: Doctrine\Common\Collections\ArrayCollection {#1029 ▶}
  -addresses: Doctrine\Common\Collections\ArrayCollection {#1488 ▶}
  -orders: Doctrine\Common\Collections\ArrayCollection {#1232 ▶}
  -ustnr: null
  -name: "test12333"
}
RegistrationController.php on line 66:
[]
RegistrationController.php on line 68:
App\Entity\Company {#1258 ▼
  -id: 5
  -comment: null
  -employees: Doctrine\Common\Collections\ArrayCollection {#1029 ▶}
  -addresses: Doctrine\Common\Collections\ArrayCollection {#1488 ▶}
  -orders: Doctrine\Common\Collections\ArrayCollection {#1232 ▶}
  -ustnr: null
  -name: "test12333"
}
RegistrationController.php on line 69:
array:1 [▼
  1258 => App\Entity\Company {#1258 ▶}
]
RegistrationController.php on line 71:
array:2 [▼
  1258 => App\Entity\Company {#1258 ▶}
  876 => App\Entity\User {#876 ▶}
]

输出platform.sh:

^ App\Entity\Company {#914 ▼
  -id: null
  -comment: null
  -employees: Doctrine\Common\Collections\ArrayCollection {#637 ▶}
  -addresses: Doctrine\Common\Collections\ArrayCollection {#913 ▶}
  -orders: Doctrine\Common\Collections\ArrayCollection {#835 ▶}
  -ustnr: null
  -name: "test1232"
}
^ []
^ App\Entity\Company {#914 ▼
  -id: null
  -comment: null
  -employees: Doctrine\Common\Collections\ArrayCollection {#637 ▶}
  -addresses: Doctrine\Common\Collections\ArrayCollection {#913 ▶}
  -orders: Doctrine\Common\Collections\ArrayCollection {#835 ▶}
  -ustnr: null
  -name: "test1232"
}
^ array:1 [▼
  914 => App\Entity\Company {#914 ▶}
]
^ array:2 [▼
  914 => App\Entity\Company {#914 ▶}
  469 => App\Entity\User {#469 ▶}
]

如您所见,在本地,我在$em->persist($company)之后生成了Company::id,但在云中,它保持为空。
如果我们删除调试代码,那么我们会得到以下错误,但仅限于platform.sh:

{"message":"Uncaught PHP Exception Doctrine\\DBAL\\Exception\\NotNullConstraintViolationException: \"An exception occurred while executing a query: SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"id\" of relation \"company\" violates not-null constraint\nDETAIL:  Failing row contains (null, null, null, Test123).\" at /app/vendor/doctrine/dbal/src/Driver/API/PostgreSQL/ExceptionConverter.php line 47","context":{"exception":{"class":"Doctrine\\DBAL\\Exception\\NotNullConstraintViolationException","message":"An exception occurred while executing a query: SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"id\" of relation \"company\" violates not-null constraint\nDETAIL:  Failing row contains (null, null, null, Test123).","code":7,"file":"/app/vendor/doctrine/dbal/src/Driver/API/PostgreSQL/ExceptionConverter.php:47","previous":{"class":"Doctrine\\DBAL\\Driver\\PDO\\Exception","message":"SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"id\" of relation
 \"company\" violates not-null constraint\nDETAIL:  Failing row contains (null, null, null, Test123).","code":7,"file":"/app/vendor/doctrine/dbal/src/Driver/PDO/Exception.php:28","previous":{"class":"PDOException","message":"SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"id\" of relation \"company\" violates not-null constraint\nDETAIL:  Failing row contains (null, null, null, Test123).","code":23502,"file":"/app/vendor/doctrine/dbal/src/Driver/PDO/Statement.php:121"}}}},"level":500,"level_name":"CRITICAL","channel":"request","datetime":"2023-05-09T12:08:20.446581+00:00","extra":{}}

数据库服务器版本(PostgreSQL 14.7)在两个环境中是相同的,而且,我认为,模式也是相同的--它是由所有迁移的自动应用程序创建的。
如何进一步调试问题并找出这种奇怪行为的原因?
更新
该命令输出一系列操作,包括对不同表重复执行以下序列:创建序列... SELECT setval(... ALTER TABLE ...我想,最重要的是:

web@app.0:~$ bin/console doctrine:schema:update --dump-sql | grep company       

CREATE SEQUENCE company_id_seq;SELECT setval('company_id_seq', (SELECT  MAX(id) FROM company));     ALTER TABLE company ALTER id SET DEFAULT nextval('company_id_seq');

在本地,它是:

symfony console doctrine:schema:update --dump-sql    

[WARNING] Not passing the "--complete" option to "orm:schema-tool:update" is deprecated and will not be supported when            using doctrine/dbal 4     

[OK] Nothing to update - your database is already in sync with the current entity metadata.

什么才是解决差异的正确方法?

doctrine:schema:update
r8xiu3jd

r8xiu3jd1#

请提供bin/console doctrine:schema:update --dump-sql在平台sh上的结果。
看起来在本地数据库而不是prod数据库上每个id上都有一个序列。
此外,如果开发人员在本地删除并重新创建迁移,如果他们手动删除表,则保持顺序。
因此,为这些表生成的任何新迁移都不会包含序列的创建。
另外,我建议你使用#[ORM\GeneratedValue('IDENTITY')]作为id,这将是下一个DBAL版本的默认值。因此,当在pgsql数据库上创建id时,它将使用SERIAL关键字代替INT,并使用单独的查询来创建序列(尝试一下)。
它将执行的操作示例:

CREATE TABLE table (id SERIAL NOT NULL)

代替

CREATE SEQUENCE table_id_seq INCREMENT BY 1 MINVALUE 1 START 1;
CREATE TABLE table (id INT NOT NULL)

-----------在评论后更新了答案----------
所以,我认为你在你的prod环境中没有序列。有两种方法可以解决。

doctrine:schema:update

但在我看来,这太危险了。第二种方法也是在生产环境中对数据库进行更改的正确方法。将prod数据库转储到本地环境中。因为你的本地数据库似乎与此无关。
https://symfony.com/bundles/DoctrineMigrationsBundle/current/index.html
它总是在两个步骤:

doctrine:migrations:diff

将生成一个文件,其中包含同步数据库所需的所有查询。然后你可以看看它的内容,以确保它是好的,并没有改变其他的东西,那些你需要的。无论如何,里面的任何事情都需要在某一天完成,但让我们专注于你的序列问题。每次看到这三个查询的组合时,您可以让它。

CREATE SEQUENCE company_id_seq; // Create the sequence
SELECT setval('company_id_seq', (SELECT  MAX(id) FROM company)); // Set the sequence to the max id of the table we will link      
ALTER TABLE company ALTER id SET DEFAULT nextval('company_id_seq'); // link the table to the sequence

如果您看到其他表的这3个查询。我建议你也把它们修好。
然后当你迁移文件只包含你所需要的。执行:

doctrine:migrations:migrate

至少你的序列问题会得到解决。但请记住一个原则:migration:diff必须为空。这意味着您的数据库与您的实体同步。

相关问题