首页 > 解决方案 > 继承表错误的 Symfony 学说迁移差异

问题描述

我对使用原则生成的差异有疑问:迁移:继承表的差异命令。我无法通过迁移命令生成这些表,而是直接使用 SQL 脚本。

这是父表的 SQL 脚本:

create table if not exists sensor
(
  id           integer    not null
    constraint sensor_pkey
    primary key,
  sensor_class integer
    constraint fk_bc8617b0ab0f4377
    references sensor_class,
  name         text,
  manufacturer text,
  status       text,
  mount        text,
  weight       numeric(10, 2) default NULL :: numeric,
  width        numeric(10, 2) default NULL :: numeric,
  height       numeric(10, 2) default NULL :: numeric,
  sensor_model integer,
  type         varchar(3) not null
);

这是一个继承表的 SQL 脚本:

create table if not exists tir
(
  no_px_height              integer,
  no_px_width               integer,
  bit_depth                 numeric(10, 2) default NULL :: numeric,
  cooled                    numeric(10, 2) default NULL :: numeric,
  radiometric               numeric(10, 2) default NULL :: numeric,
  single_shot_max_data_rage numeric(10, 2) default NULL :: numeric,
  detector_size_h           numeric(10, 2) default NULL :: numeric,
  detector_size_w           numeric(10, 2) default NULL :: numeric,
  fov_h                     numeric(10, 2) default NULL :: numeric,
  fov_w                     numeric(10, 2) default NULL :: numeric,
  focal_length              numeric(10, 2) default NULL :: numeric,
  band_width_min            numeric(10, 2) default NULL :: numeric,
  band_width_max            numeric(10, 2) default NULL :: numeric,
  min_time_between_images   numeric(10, 2) default NULL :: numeric
)
  inherits (sensor);

我有其他从传感器继承的表,这些表在传感器实体的区分图中指示。

这是传感器实体:

/**
 * Sensor
 *
 * @ORM\Table(name="sensor")
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string", length=3)
 * @ORM\DiscriminatorMap({
 *     "TIR"="Tir",
 *     "RGB"="Rgb",
 *     "MLS"="Multispectral",
 *     "EOC"="VideoCamera"
 * })
 */
abstract class Sensor
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     */
    public $id;

    /**
     * @var string|null
     *
     * @ORM\Column(name="name", type="text", nullable=true)
     */
    public $name;

    /**
     * @var string|null
     *
     * @ORM\Column(name="manufacturer", type="text", nullable=true)
     */
    public $manufacturer;

    /**
     * @var int|null
     *
     * @ORM\Column(name="sensor_model", type="integer", nullable=true)
     */
    public $sensorModel;
others fields...

这是继承实体之一:

/**
 * @ORM\Entity
 * @ORM\Table(name="tir")
 */
class Tir extends Sensor
{
    /**
     * @var int|null
     *
     * @ORM\Column(name="no_px_height", type="integer", nullable=true)
     */
    private $noPxHeight;

    /**
     * @var int|null
     *
     * @ORM\Column(name="no_px_width", type="integer", nullable=true)
     */
    private $noPxWidth;

    /**
     * @var float|null
     *
     * @ORM\Column(name="bit_depth", type="decimal", precision=10, scale=2, nullable=true)
     */
    private $bitDepth;
others fields.

只有未继承的字段。

迁移差异输出:

$this->addSql('ALTER TABLE tir DROP sensor_class');
$this->addSql('ALTER TABLE tir DROP name');
$this->addSql('ALTER TABLE tir DROP manufacturer');
$this->addSql('ALTER TABLE tir DROP status');
$this->addSql('ALTER TABLE tir DROP mount');
$this->addSql('ALTER TABLE tir DROP weight');
$this->addSql('ALTER TABLE tir DROP width');
$this->addSql('ALTER TABLE tir DROP height');
$this->addSql('ALTER TABLE tir DROP sensor_model');
$this->addSql('ALTER TABLE tir DROP type');
$this->addSql('ALTER TABLE tir ADD CONSTRAINT FK_A68DD960BF396750     FOREIGN KEY (id) REFERENCES sensor (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE tir ADD PRIMARY KEY (id)');

如何防止这些差异?

标签: symfonydoctrine-ormdoctrine

解决方案


推荐阅读