【问题标题】:Reference the name instead of the id引用名称而不是 id
【发布时间】:2020-07-22 14:46:18
【问题描述】:

伙计们,我有一个我还没有解决的问题,我在网上没有找到任何东西,我需要在我的表中引用字段 nome_mulher 而不是表 'women' 的字段 id在我的另一个表“侵略者”中,或者是否可以在“侵略者”表中引用这两个字段。

表攻击者

public function up()
{
    Schema::create('agressors', function (Blueprint $table) {

        $table->engine = 'InnoDB';
        $table->bigIncrements('id');
        $table->unsignedBigInteger('mulher_id');
        $table->foreign('mulher_id')->references('id')->on('mulhers');
        $table->string('nome')->nullable();
        $table->string('idade')->nullable();
        $table->string('vinculo')->nullable();
        $table->string('escolaridade')->nullable();
        $table->string('rua_agressor')->nullable();
        $table->string('bairro_agressor')->nullable();
        $table->string('numero_agressor')->nullable();
        $table->string('profissao')->nullable();
        $table->string('tipo_de_agressão')->nullable();
        $table->string('primeira_agressao')->nullable();
        $table->string('vezes_de_agressao')->nullable();
        $table->string('boletim_de_ocorrencia')->nullable();
        $table->string('numero_boletim')->nullable();
        $table->string('dependencia_quimica_agressor')->nullable();
        $table->string('qual_quimico_agressor')->nullable();
        $table->string('passagem_prisional_agressor')->nullable();
        $table->string('tempo')->nullable();

        $table->timestamps();
    });
}

餐桌铣刀

public function up()
{
    Schema::create('mulhers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigIncrements('id');
        $table->string('nome_mulher')->nullable();
        $table->string('nome_social')->nullable();
        $table->string('primeiro_registro')->nullable();
        $table->string('data_de_registro')->nullable();
        $table->string('nome1')->notnullable()->change();
        $table->date('data_de_nascimento')->nullable();
        $table->string('cpf')->nullable();
        $table->string('pai')->nullable();
        $table->string('mae')->nullable();
        $table->string('etnia')->nullable();
        $table->string('religiao')->nullable();
        $table->boolean('possui_bolsa_familia')->nullable();
        $table->string('celular')->nullable();
        $table->string('tel_fixo')->nullable();
        $table->string('beneficio_social')->nullable();
        $table->string('renda')->nullable();
        $table->string('pensao_alimenticia')->nullable();
        $table->boolean('carteira_de_trabalho')->nullable();
        $table->boolean('dependente_quimico')->nullable();
        $table->string('qual_quimico')->nullable();
        $table->boolean('medicacao')->nullable();
        $table->string('qual_medicacao')->nullable();
        $table->boolean('pessoa_com_deficiencia_fisica')->nullable();
        $table->string('qual_deficiencia_fisica')->nullable();
        $table->string('tipo_cancer')->nullable();
        $table->boolean('possui_cancer')->nullable();
        $table->boolean('trans_mental')->nullable();
        $table->string('qual_trans_mental')->nullable();
        $table->string('hiv')->nullable();
        $table->boolean('possui_passagem')->nullable();
        $table->string('tempo_de_passagem')->nullable();
        $table->string('rua')->nullable();
        $table->string('numero')->nullable();
        $table->string('bairro')->nullable();
        $table->string('situacao_moradia')->nullable();
        $table->string('estado_civil')->nullable();
        $table->boolean('possui_filhos')->nullable();
        $table->string('quantos_filhos')->nullable();
        $table->string('escolaridade')->nullable();
        $table->string('profissao')->nullable();
        $table->string('genero')->nullable();
        $table->string('cidade_da_mulher')->nullable();
        $table->string('id_genero')->nullable();
        $table->string('sexualidade')->nullable();
        $table->string('sexo')->nullable();
        $table->boolean('gestante')->nullable();
        $table->string('id_gestante')->nullabel();

        $table->timestamps();
    });
}

【问题讨论】:

  • 你需要在agressors之前迁移mulhers
  • 另外,如果这些是高度相关的表,您可以将两个Schema::create 语句放在一个迁移up() 方法中。
  • 您对此有何疑问? “参考”是什么意思?
  • 非常感谢您的每一个帮助,我很感激,我正在尝试这个解决方案

标签: php mysql laravel


【解决方案1】:

如果外键没有引用父表上的主键,则它必须引用唯一键。 Source

这就是为什么您需要更改 mulhers 表的原因:

$table->string('nome_mulher')->nullable()->unique();

在您的agressors 表中将行更改为:

$table->string('nome_mulher');
$table->foreign('nome_mulher')->references('nome_mulher')->on('mulhers');

之后,先迁移mulhers表,再迁移agressors表。

【讨论】:

  • 非常感谢您的帮助,我正在尝试这个解决方案
猜你喜欢
  • 2021-04-26
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多