yajra / laravel-oci8

Oracle DB driver for Laravel via OCI8
https://yajrabox.com/docs/laravel-oci8
MIT License
834 stars 237 forks source link

Laravel does not delete bank record #318

Closed danilodorgam closed 2 years ago

danilodorgam commented 7 years ago

Summary of problem or feature request

The laravel does not show any error but also does not delete.

Code snippet of problem

Controller

 public function destroy($id)
    {
        //
        $projeto = ProjetoModel::findOrFail($id);
        return response()->json($projeto->delete());
    }

Model

class ProjetoModel extends Model
{
    //
    use SoftDeletes;
    const  DELETED_AT = 'ST_EXCLUIDO';
    const  CREATED_AT = 'ST_CRIADO';
    const  UPDATED_AT = 'ST_ATUALIZADO';
    const NAME_TABLE = 'PROJETO';
    protected $table = 'PROJETO';
    protected $primaryKey = 'ID_PROJETO';
    protected $fillable = ['NM_PROJETO','SG_PROJETO','ID_RESPONSAVEL'];
    protected $guarded = ['ID_PROJETO','ST_EXCLUIDO','ST_CRIADO', 'ST_ATUALIZADO'];
}

System details

mstaack commented 7 years ago

@danilodorgam what does dd($projeto) give you? do you get a result?

danilodorgam commented 7 years ago

@mstaack


ProjetoModel {#247
  #table: "PROJETO"
  #primaryKey: "ID_PROJETO"
  #fillable: array:3 [
    0 => "NM_PROJETO"
    1 => "SG_PROJETO"
    2 => "ID_RESPONSAVEL"
  ]
  #guarded: array:4 [
    0 => "ID_PROJETO"
    1 => "ST_EXCLUIDO"
    2 => "ST_CRIADO"
    3 => "ST_ATUALIZADO"
  ]
  #connection: null
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [
    "id_projeto" => "2"
    "nm_projeto" => "PF"
    "sg_projeto" => "pf"
    "id_responsavel" => "1"
    "st_criado" => "2017-07-26 19:14:02"
    "st_atualizado" => "2017-07-27 19:14:02"
    "st_excluido" => null
  ]
  #original: array:7 [
    "id_projeto" => "2"
    "nm_projeto" => "PF"
    "sg_projeto" => "pf"
    "id_responsavel" => "1"
    "st_criado" => "2017-07-26 19:14:02"
    "st_atualizado" => "2017-07-27 19:14:02"
    "st_excluido" => null
  ]
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
}
mstaack commented 7 years ago

try looking what sql is produced and on which connection u are making the call..

try

\DB::listen(function($sql) {
    var_dump($sql);
});

in appserviceprovider

and then check your sql...

mstaack commented 7 years ago

@danilodorgam also keep in mind you are using SoftDelete Trait!

u can also install https://github.com/barryvdh/laravel-debugbar to have some better overview whats going on your db side :)

danilodorgam commented 7 years ago

Yes, but the DELETED_AT field remains null.

I realized that the same happens when I try to update the registry. It does not change.

mstaack commented 7 years ago

check your logs... what return value do you get from the delete call? if none were delete it should be 0...

mstaack commented 7 years ago

maybe this is related to https://github.com/yajra/laravel-oci8/issues/312

danilodorgam commented 7 years ago

27-07-2017 16-44-53

mstaack commented 7 years ago

give running a manual delete via laravel query builder a try

danilodorgam commented 7 years ago

DB::table('projeto')->where('ID_PROJETO', '=', 3)->delete(); So physically deleted from the bank

jchn-codes commented 6 years ago

manually deleting as @mstaack noted works fine, but I don't like it, because I have to type the table name in lots of queries instead of using the protected $table property on the model.

Neither Project::findOrFail($id)->delete() nor Project::destroy($id) works.

yajra commented 6 years ago

Delete works fine on all my projects. Maybe you have some event / model listeners that cancels the delete command?

jchn-codes commented 6 years ago

I found the solution.. I've overridden the primary key in my model protected $primaryKey = 'IDNR' (columnname uppercase). In Eloquent\Model the function setKeysForSaveQuery() fetches the key which returned null in my case. After changing protected $primaryKey = 'idnr' (lowercase) both, deleting and updating, works as expected.

However, it's still weird it returns "true" even if I tried to delete a db record where IDNR = null. It could be a correct Statement, but only if I pass null as value. Also every SELECT and INSERT works with uppercase primary key.

almerleoalmazan commented 3 years ago

Thanks @jchn-codes, you really saved my day! 👍