-1

i"m using laravel+Nova, connecting to existing database of an existing software. The primary key is not the deault "ID" so at the "model" i changed it to like that:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class products extends Model
{
    //
    protected $table = 't_items';
    protected $primaryKey = 'item_num';

    public $timestamps = false;

}

============================================================= When changing and updating the satabase (With nova) there is no error. However when using save (for new or existing row) there is an error:

SQLSTATE[42000]:[Microsoft][ODBC Driver 13 forSQL Server][SQL Server] Error converting data type nvarchar to numeric 9SQL select top 1 * drom [t_items][item_num] = null

"item_num" is defined as:
numeric
primary key
Allo nulls = false
Identity = false
Identity seed = 0
Identity increment = 0
Length = 9
Numeric precision = 10
Numeric scale = 0

What should i add. i think laravel soed not defining the ID when saving and the server needs it

David Gabbay
  • 107
  • 8
  • Hi David, i saw `Error converting data type nvarchar to numeric` in the error message. could you turn off the auto increments of the primary key using [`$incrementing = false` on the model](https://laravel.com/docs/5.7/eloquent)? – Bagus Tesa Dec 31 '18 at 23:15
  • i tried it. still get this error: SQLSTATE[42000]:[Microsoft][ODBC Driver 13 for SQL Server][SQL Server] Cannot insert the value NULL into column 'item_num'.... – David Gabbay Jan 01 '19 at 06:26
  • `Cannot insert the value NULL into column 'item_num'.` hold on David, could you post how you insert the data? after settings `$incrementing` to `false` you had always to assign the primary key manually whenever you save new model.. – Bagus Tesa Jan 01 '19 at 07:35
  • nova does not ask for id when create a new row. i dont fined how to make it ask for it. Though i added required to this field at App/Nova/Produccts php like so: public function fields(Request $request) { return [ ID::make('ID', 'item_num') ->creationRules('required') ->sortable(), Text::make('item_name') ]; } – David Gabbay Jan 01 '19 at 22:45
  • i now see this issue happened to others but was not answered nor solved at github: https://github.com/laravel/nova-issues/issues/734 – David Gabbay Jan 01 '19 at 22:47
  • my bad, seems Nova source is not open and require subscription to use. since we have been shooting in the dark for too long. i could only propose to hook [event handling](https://laravel.com/docs/5.7/eloquent#events) on the model to trap creating event and allow you to set the primary key manually. or you could dig the `ID` class by yourselves to see if we can do some magic on it. – Bagus Tesa Jan 01 '19 at 23:03

1 Answers1

0

You have to set the column to allow null. This is nothing to do with Laravel. Though it is a pretty strange situation for you not wanting an incrementing id to be set simply as in the "id" column.

John
  • 196
  • 7
  • i dont want to change the defenitions of the database since i"m using an existing database of an existing software. – David Gabbay Jan 01 '19 at 06:15