vincent15000's avatar

Missing column in a database view while executing a query

Hello,

I have this Livewire component.

I can select a user to get only the commissions for a specific user.

Some users don't have any commissions.

To reproduce the error, here are the steps :

  • I select a user who doesn't have any commission

  • I select a user who has commissions

  • I select a user who doesn't have any commission => here is the error

  • I select a user who has commissions => now I get always the error, even if the user has commissions

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'month_commissions_view.id' in 'where clause' (Connection: mariadb, SQL: select * from month_commissions_view where month_commissions_view.id in (0, 0, 0, 0))

So I get the error only for a user who doesn't have any commission, but only if I have retrieved the commissions for a user who has commissions.

Any idea why ?

Thanks for your help.

V

0 likes
12 replies
vincent15000's avatar

If I have this code, I don't have any error any more.

Why ?

class Commissions extends Component
{
    public $users;
    public $selected_user_id;

    public function mount()
    {
        $this->users = User::
            where('company_id', auth()->user()->company_id)
            ->orderBy('name')
            ->get();
    }

    public function render()
    {
        $commissions = MonthCommission::
            with('user')
            ->where('user_id', $this->selected_user_id)
            ->whereNotNull('month')
            ->orderByDesc('month')
            ->get();

        return view('livewire.commissions', compact('commissions'));
    }
}
vincent15000's avatar

@Tray2 I have 2 views, but for this query, it's only with one view. I have create the MonthCommission model to be binded to the database view.

Tray2's avatar

@vincent15000 Then in my book it shouldn't be possible for that column to be missing.

1 like
vincent15000's avatar

@Tray2 In the view, I don't have the commissions.id column, but I don't need it.

vincent15000's avatar

@Tray2 If I add it, I need to add it to group by and I don't get the same result any more.

vincent15000's avatar

@Tray2

            CREATE OR REPLACE VIEW month_commissions_view 
            AS
            SELECT
                users.name AS user_name,
                users.company_id AS company_id,
                commissions.year AS year,
                commissions.month AS month,
                CONCAT(commissions.year, '-', LPAD(commissions.month, 2, '0')) AS year_and_month,
                SUM(CASE WHEN commissions.sale_id IS NOT NULL THEN commissions.amount ELSE 0 END) AS sale_based_amount,
                SUM(CASE WHEN commissions.sale_id IS NULL THEN commissions.amount ELSE 0 END) AS month_based_amount,
                SUM(commissions.amount) AS total_amount
            FROM
                users
                LEFT JOIN commissions ON commissions.user_id = users.id
            GROUP BY users.name, users.company_id, commissions.year, commissions.month
Tray2's avatar

@vincent15000 I don't see any reason for this, other than it might be that the public $commissions doesn't get reset properly.

What happens if you set that to null before you call getComissions?

 public function mount()
    {
		$this->comissions = null;
        $this->getCommissions();

        $this->users = User::
            where('company_id', auth()->user()->company_id)
            ->orderBy('name')
            ->get();
    }
Snapey's avatar

dont grab your user model and put it in a public property.

The whole user model will be shared and visible in the browser

2 likes
vincent15000's avatar

@Snapey I need the users' list to put it in a select / options field. Do you suggest me to select only the needed fields (id and name) ? Or simply to load it inside the render function and pass it as a parameter to the view ?

Please or to participate in this conversation.