#belongsToMany column cannot be null

24 messages · Page 1 of 1 (latest)

slim surge
#

Hi

I have a database, which is defined by external sources, so structure cannot be amended.

I have a users table, which links to a reply table using username field on both tables. Then on the reply table is a value table, which links to groups.name

I have setup the relations, and doing a get on users and groups with the relation shows the related results.

Now, I have a RelationManager for Groups on the User resource, but when I try to attach groups, I just get an error that "Column 'value' cannot be null".

Has anyone come across this in the past, and if so, know how I can solve this. I suspect it's because of the non-standard column names, but I've got the relationships working fine.

neon mauveBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

mossy vector
#

You probably need to set the primary keys for each model if they are different, but also doing that in the relaitonship too.

#

the cannot be null, means a value is being passed that is null that cannot be. So if it's an empty column and you want it to be empty, you will need to ensure on saving you cast it to empty opposed to null.

slim surge
#

So, that's something that makes sense. Thought in the case of this DB, the primary keys are not the ones that go in the pivot table.

it's not straight forward, but this is a DB structure another bit of software uses, so I'm trying to get Laravel to be able to manage the data within the DB.

If it helps at all, this is shortened diagram showing the columns that are relevent to this

mossy vector
#

So its not performance but you can make the username match across. then the groups for the keys.

value is saying it cannot be empty which is true. So the value then maps to the groups.

It looks to me your value on the radreply relationship needs to be set as the group.name. That would solve your issue then

slim surge
#

Yeah, I'm using a BelongsToMany relationship (both ways as it's Many to Many)

#

and the relationship works, like if I do a RadCheck::with('groups')->first()

I see the groups

#

and likewise

Group::with('radcheck')->first()

I see the checks that are related

#

I mean, if there's a simpler way to do it without the groups table - I don't mind using an alternative method

mossy vector
slim surge
#

yeah, I got the relation manager setup

#

and I can see related groups fine, it's just when I attach i get the avove error, and also when I detatch, it deos nothing.

mossy vector
#

and you set the key as value -> name for the groups ?

slim surge
#

on groups "id" column is the primary key, though I do want it to select name, so maybe I change that

mossy vector
#

There you go, it's maping across as ID but everything on the modle is using name to link

slim surge
#

OK, just removed the ID column, and made name primary, now getting an error that it can't sort by groups.id

mossy vector
#

Sort by name instead

slim surge
#

Oh, is that a default attribute on the model?

#

I'm not specifying any sort myself

#

OK, so I'm back to where I was before getting value cannot be null

#

but with just name on the groups column

#

groups table*

slim surge
#

Well, this is odd, I've just done this

Route::get('/', function (\Illuminate\Http\Request $request) {
    $g = \App\Models\Radius\Group::where('name', '=', 'Test 2')
        ->first();
    $c = \App\Models\Radius\Check::first();

    $c->groups()->attach('Test 2');
    dd($g, $c);
});

and that attaches the model fine, but if I change the argument passed to attach() to $g, it gets the value cannot be null again