mardi 29 mars 2016

Can anyone advice me how to treat when the table has no primary key

I have a table as follows:

| common_menus | CREATE TABLE `common_menus` (
  `menu_id` tinyint(3) unsigned NOT NULL,
  `branch_id` smallint(5) unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `price` smallint(5) unsigned NOT NULL,
  `type` tinyint(3) unsigned NOT NULL COMMENT '0 - 255',
  `order` tinyint(3) unsigned NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |

The model corresponding to this table is as follows:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Menu extends Model
{
  use SoftDeletes;
  const TABLE = 'common_menus';

  const MENU_ID = 'menu_id';
  const BRANCH_ID = 'branch_id';
  const NAME = 'name';
  const PRICE = 'price';
  const TYPE = 'type';
  const ORDER = 'order';

  const TABLE_MENU_ID = self::TABLE . '.' . self::MENU_ID;
  const TABLE_BRANCH_ID = self::TABLE . '.' . self::BRANCH_ID;
  const TABLE_NAME = self::TABLE . '.' . self::NAME;
  const TABLE_PRICE = self::TABLE . '.' . self::PRICE;
  const TABLE_TYPE = self::TABLE . '.' . self::TYPE;
  const TABLE_ORDER = self::TABLE . '.' . self::ORDER;

  protected $table = self::TABLE;
  protected $dates = ['deleted_at'];
  protected $fillable = [self::MENU_ID, self::BRANCH_ID, self::NAME, self::PRICE, self::TYPE, self::ORDER];

  const STANDARD_MENU = 1;
  const COMBO_MENU = 2;
  const OPTION_MENU = 3;

  // static methods follows

}

I encountered an error as follow when the method to delete the model was called:

QueryException in Connection.php line 651:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `common_menus` set `deleted_at` = 2016-03-29 14:01:01 where `id` is null)

I found that I should declare as follows whenever the table has no primary key.

  protected $primaryKey = null;

Ok, I also have the different but similar table as follows:

| common_therapists | CREATE TABLE `common_therapists` (
  `therapist_id` int(10) unsigned NOT NULL COMMENT '0 - 4294967295',
  `branch_id` smallint(5) unsigned NOT NULL,
  `name` varchar(63) COLLATE utf8mb4_unicode_ci NOT NULL,
  `has_license` tinyint(1) unsigned DEFAULT NULL,
  `lmt` varchar(5) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |

The model corresponding to this table is as follows:

<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Therapist extends Model
{
  use SoftDeletes;
  const TABLE = 'common_therapists';
//  const KEY = 'therapist_id';
//  const THERAPIST_ID = self::KEY;
  const THERAPIST_ID = 'therapist_id';
  const TABLE_THERAPIST_ID = self::TABLE . '.' . self::THERAPIST_ID;
  const BRANCH_ID = 'branch_id';
  const TABLE_BRANCH_ID = self::TABLE . '.' . self::BRANCH_ID;
  const NAME = 'name';
  const TABLE_THERAPIST_NAME = self::TABLE . '.' . self::NAME;
  const DELETED_AT = 'deleted_at';
  const MASSSAGE_THERAPIST = 0;
  const LICENSED_MASSAGE_THERAPIST = 1;

  protected $table = self::TABLE;
//  protected $primaryKey = self::KEY;
  protected $dates = ['deleted_at'];
  protected $perPage = 10;
  protected $fillable = ['therapist_id', 'branch_id', 'name', 'has_license', 'lmt'];

  // static methods follows
}

Before this model and table had primary key but it was removed because of requirement changes.

The table and model to treat therapists works completely even it has no declaration of primary key.

My question is whether the table and the model of menus has a problem, or the table and the model of therapists is working wrongly?

Please specify the problem or my misunderstanding. Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire