laravel / jetstream

Tailwind scaffolding for the Laravel framework.
https://jetstream.laravel.com
MIT License
3.96k stars 813 forks source link

Creating User Manually With Jetstream Teams SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myappdb.App\Models\Membership' doesn't exist #1002

Closed kinger251285 closed 2 years ago

kinger251285 commented 2 years ago

Description:

I am creating a new user using the non register route. I am manually adding a new user. No team is created on basic users (no team is required - this should be a feature IMO). I get an error stating - "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myappdb.App\Models\Membership' doesn't exist".

I can see this is caused either by the HasTeams() on the User Model or vendor files shared Inertia Data Middleware - "'user' => function () use ($request) {" this section.

Commenting the middleware section out, the error disappears. Commenting the User Model HasTeams() out changes the errror.

I have also tried creating a team when manually creating the user. I have also manually (in the DB) tried adding a team and still get the error.

I cannot see anywhere why it would be referencing Membership rather than teams.

My Code:

UserCreation: `

    $Email = $request->email;
    $User = User::where('email', $Email)->first();

    if($User == null || $User == ''){

        $User = new User();
        $User->email = $Email;

    }

    if(!$User->subscribed('default')){

        $User->name = $request->first_name;
        $User->first_name = $request->first_name;
        $User->last_name = $request->last_name;
        $User->password = Hash::make($request->password);
        $User->country = $request->country;
        $User->telephone = $request->phone;
        $User->admin_role = 'something';

        $User->save();

        if($User->allTeams()->isEmpty()){

            $team = $User->ownedTeams()->create([
                'user_id' => $User->id,
                'name' => explode(' ', $User->first_name, 2)[0]."'s Team",
                'personal_team' => true,
            ]);

        } 

        Auth::login($User);

        return redirect()->route('some_route');

    }`

SharedInertiaData Code Which Seems to be causing the issue: `

            user' => function () use ($request) {
            if (! $request->user()) {
                return;
            }
            if (Jetstream::hasTeamFeatures() && $request->user()) {
                $request->user()->currentTeam;
            }
            return array_merge($request->user()->toArray(), array_filter([
                'all_teams' => Jetstream::hasTeamFeatures() ? $request->user()->allTeams()->values() : null,
            ]), [
                'two_factor_enabled' => ! is_null($request->user()->two_factor_secret),
            ]);
        },`
kinger251285 commented 2 years ago

Ive just also tested a new sign up via the standard out of the box register route and having the same issue

kinger251285 commented 2 years ago

This line specifically is breaking this. It does not like the allTeams() function. Im not sure why. Checking an older version of jetstream it looks like the ->values() is a recent addition.

vendor/laravel/jetstream/src/http/middleware/shareinertiadata.php

'all_teams' => Jetstream::hasTeamFeatures() ? $request->user()->allTeams()->values() : null,

driesvints commented 2 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

kinger251285 commented 2 years ago

@driesvints

Im not sure how this is not a bug with the library. The issue is stemming from the Jetstream package, SharedInertiaData. Ive not had this issue in other projects and have recently updated the Jetstream package on this specific project.

driesvints commented 2 years ago

Every user requires a team. A personal team is always required.

kinger251285 commented 2 years ago

@driesvints

I get that. However even when the team is created and a personal team is allocated, either via the code the same way a registered user is or manually via input into the database this error occurs.

A user created via the bog standard out of the box register form is also creating this error.

kinger251285 commented 2 years ago

the issue seams to be with allTeams()

driesvints commented 2 years ago

Heya, thanks for reporting.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as separate commits on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

driesvints commented 2 years ago

I'll reopen this once you've provided a repo.

kinger251285 commented 2 years ago

@driesvints Can i give you access privately permission to my bitbucket repo? This is in a partially live site.

driesvints commented 2 years ago

@kinger251285 it's best that you try to reproduce this in a fresh Laravel app with the command from above. If you can't, then I suspect this is something specifically for your app.

kinger251285 commented 2 years ago

@driesvints No problem, i think you are right, ive copied the Jetstream vendor files from a working project i know all is working. I cannot fathom why in this project it thinks that membership is its own table rather than act as a pivot. Its all down to the allTeams() function.

kinger251285 commented 2 years ago

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myDBName.App\Models\Membership' doesn't exist (SQL: select teams.*, App\Models\Membership.user_id as pivot_user_id, App\Models\Membership.team_id as pivot_team_id, App\Models\Membership.role as pivot_role, App\Models\Membership.created_at as pivot_created_at, App\Models\Membership.updated_at as pivot_updated_at from teams inner join App\Models\Membership on teams.id = App\Models\Membership.team_id where App\Models\Membership.user_id = 1)

Is the full error i get.

kinger251285 commented 2 years ago

Its anytime allTeams() is accessed not just creating a new user manually.

kinger251285 commented 2 years ago

FIXED FIXED FIXED - After installing a new laravel install i noticed my existing project was missing the App\Models\Membership. Thinking this was being accessed via the vendor package files. It appears i may have updated Jetstream somewhere and this model had not been published correctly. Copied it across and now allTeams() works.