簡體   English   中英

laravel將可空列更改為外鍵中無可空

[英]laravel change nullable column to no nullable in foreign key

這是我的遷移:

        $table->increments('id');
        $table->string('nombre1',255);
        $table->string('nombre2',255)->nullable();
        $table->string('apellido1',255)->nullable();
        $table->string('apellido2',255)->nullable();
        $table->string('apellido_casada',255)->nullable();
        $table->string('cedula',255)->nullable();
        $table->integer('entidad_id')->nulleable()->unsigned();
        **$table->integer('cargo_id')->nulleable()->unsigned();
        $table->integer('partido_id')->nulleable()->unsigned();
        $table->integer('provincia_id')->nulleable()->unsigned();
        $table->integer('distrito_id')->nulleable()->unsigned();
        $table->integer('corregimiento_id')->nulleable()->unsigned();**
        $table->string('otro_nombre',255)->nullable();
        $table->dateTime('fecha_nacimiento')->nullable();
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('entidad_id')->references('id')->on('entidad');
        $table->foreign('cargo_id')->references('id')->on('cargo');
        $table->foreign('partido_id')->references('id')->on('partido');
        $table->foreign('provincia_id')->references('id')->on('provincia');
        $table->foreign('distrito_id')->references('id')->on('distrito');
        $table->foreign('corregimiento_id')->references('id')->on('corregimiento'); 

當我運行此遷移時,生成的表為:

    CREATE TABLE pep (
    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    nombre1 VARCHAR(255) COLLATE UTF8_UNICODE_CI NOT NULL,
    nombre2 VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
    apellido1 VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
    apellido2 VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
    apellido_casada VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
    cedula VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
    **entidad_id INT(10) UNSIGNED NOT NULL,
    cargo_id INT(10) UNSIGNED NOT NULL,
    partido_id INT(10) UNSIGNED NOT NULL,
    provincia_id INT(10) UNSIGNED NOT NULL,
    distrito_id INT(10) UNSIGNED NOT NULL,
    corregimiento_id INT(10) UNSIGNED NOT NULL,**
    otro_nombre VARCHAR(255) COLLATE UTF8_UNICODE_CI DEFAULT NULL,
    fecha_nacimiento DATETIME DEFAULT NULL,
    created_at TIMESTAMP NULL DEFAULT NULL,
    updated_at TIMESTAMP NULL DEFAULT NULL,
    deleted_at TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (id),
    KEY pep_entidad_id_foreign (entidad_id),
    KEY pep_cargo_id_foreign (cargo_id),
    KEY pep_partido_id_foreign (partido_id),
    KEY pep_provincia_id_foreign (provincia_id),
    KEY pep_distrito_id_foreign (distrito_id),
    KEY pep_corregimiento_id_foreign (corregimiento_id),
    CONSTRAINT pep_cargo_id_foreign FOREIGN KEY (cargo_id)
        REFERENCES cargo (id),
    CONSTRAINT pep_corregimiento_id_foreign FOREIGN KEY (corregimiento_id)
        REFERENCES corregimiento (id),
    CONSTRAINT pep_distrito_id_foreign FOREIGN KEY (distrito_id)
        REFERENCES distrito (id),
    CONSTRAINT pep_entidad_id_foreign FOREIGN KEY (entidad_id)
        REFERENCES entidad (id),
    CONSTRAINT pep_partido_id_foreign FOREIGN KEY (partido_id)
        REFERENCES partido (id),
    CONSTRAINT pep_provincia_id_foreign FOREIGN KEY (provincia_id)
        REFERENCES provincia (id)
)  ENGINE=INNODB DEFAULT CHARSET=UTF8 COLLATE = UTF8_UNICODE_CI;

問題是某些列:entidad_id,cargo_id等聲明為NULL,當我設置FK時,laravel將其創建為NOT NULL。

我想念這里嗎?

對不起,來晚了。

您在代碼中有錯別字

nulleable

$table->integer('entidad_id')->nulleable()->unsigned();

應該

nullable

$table->integer('entidad_id')->nullable()->unsigned();

不知道Laravel為什么不大喊大叫:(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM