sqlite: 修复装载 null 而不是字符串 – NOT NULL 约束失败

huangapple go评论61阅读模式
英文:

sqlite: Fixtures load null instead of string - NOT NULL constraint failed

问题

以下是您提供的代码的翻译部分:

我有一个简单的实体,只包含一个字段 "name":

/**
 * @ORM\Table(name="gender")
 * @ORM\Entity
 * @codeCoverageIgnore
 */
class Gender
{
    public const FEMALE = 'female';
    public const MALE = 'male';
    public const OTHER = 'other';

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=15, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private string $name;

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): Gender
    {
        $this->name = $name;

        return $this;
    }
}

对于这个类,我尝试生成 Fixture 并将它们插入到用于测试的 SQLite 数据库中:

use App\Entity\Gender;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class GenderFixtures extends Fixture
{

    public function load(ObjectManager $manager)
    {
        $genders = [Gender::FEMALE, Gender::MALE, Gender::OTHER];

        foreach ($genders as $genderName) {
            echo $genderName . PHP_EOL;

            $gender = (new Gender())
                ->setName($genderName);
            $manager->persist($gender);
        }

        $manager->flush();
    }
}

当我调用以下命令时:

bin/console  doctrine:fixtures:load --env=test

我得到以下错误信息:

female
male
other

In AbstractSQLiteDriver.php line 39:

An exception occurred while executing 'INSERT INTO gender (name) VALUES (null)':

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: gender.name

所以您可以看到,这里有三个我想要用作名称的字符串。为什么 Fixture 在插入语句中没有使用它们?

Symfony 4.4,PHP 7.4.1,Doctrine Fixtures 3.3.0

如果您需要更多帮助或解释,请告诉我。

英文:

I have a simple Entity consisting of just one field "name":

/**
 * @ORM\Table(name="gender")
 * @ORM\Entity
 * @codeCoverageIgnore
 */
class Gender
{
    public const FEMALE = 'female';
    public const MALE = 'male';
    public const OTHER = 'other';

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=15, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private string $name;

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): Gender
    {
        $this->name = $name;

        return $this;
    }
}

For this class I try to generate fixtures and insert them into a sqlite db for testing:

use App\Entity\Gender;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class GenderFixtures extends Fixture
{

    public function load(ObjectManager $manager)
    {
        $genders = [Gender::FEMALE, Gender::MALE, Gender::OTHER];

        foreach ($genders as $genderName) {
            echo $genderName . PHP_EOL;

            $gender = (new Gender())
                ->setName($genderName);
            $manager->persist($gender);
        }

        $manager->flush();
    }
}

When I call

 bin/console  doctrine:fixtures:load --env=test

I get this:

female
male
other

In AbstractSQLiteDriver.php line 39:
                                                                                               
  An exception occurred while executing 'INSERT INTO gender (name) VALUES (null)':             
                                                                                               
  SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: gender.name 

So as you can see there are the three strings I want to use as name. Why are they not used by the Fixture in the insert statement?

Symfony 4.4, PHP 7.4.1, Doctrine Fixtures 3.3.0

答案1

得分: 1

It seems like fixtures filling a sqlite db cannot deal with primary keys, which are not auto incremented integers.

The same fixtures worked on a MySql database like a charm. So as long as I want to use the sqlite db for unittests, I added to all my entities

/**

  • @ORM\Id()
  • @ORM\GeneratedValue()
  • @ORM\Column(type="smallint", options={"unsigned":true})
  • @ORM\GeneratedValue(strategy="IDENTITY")
    */
    private ?int $id = null;

/**

  • @var string
  • @ORM\Column(name="name", type="string", length=15, nullable=false)
    */
    private string $name;

And of course I had to change the naming and the referenced column for the connected entities.

Still I don't know if it is wise to use a different db for testing than for dev and prod. Problems like this show that it is not an ideal solution.

When I did run a similar application locally, I just had another MySql db for testing. Now I run this app in docker and had troubles in using a different db for tests, than for dev. Hence the sqlite solution....

英文:

It seems like fixtures filling a sqlite db cannot deal with primary keys, which are not auto incremented integers.

The same fixtures worked on a MySql database like a charm. So as long as I want to use the sqlite db for unittests, I added to all my entities

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="smallint", options={"unsigned":true})
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private ?int $id = null;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=15, nullable=false)
 */
private string $name;

And of course I had to change the naming and the referenced column for the connected entities.

Still I don't know if it is wise to use a different db for testing than for dev and prod. Problems like this show that it is not an ideal solution.

When I did run a similar application locally, I just had another MySql db for testing. Now I run this app in docker and had troubles in using a different db for tests, than for dev. Hence the sqlite solution....

huangapple
  • 本文由 发表于 2020年1月3日 21:33:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定