Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

denewey's avatar

PHP Artisan Tinker ERROR relation [pivot_table] does not exist

I have a 'clients' table and a 'messages' table'. These two have a ManyToMany relationship, so I created a pivot table called 'client_message'. The 'clients' and 'messages' tables were already existing in our Postgres database so I didn't have to create them using a migration. I also created the 'client_message' table directly in the database without using a migration.

The 'clients' table has a custom id called 'clients_id' so I had to set the primaryKey:

protected $primaryKey = 'clients_id';

Thsi is my 'clients' table structure:

CREATE TABLE IF NOT EXISTS spot.clients
(
    clients_id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    message_id integer NOT NULL,
    mac character varying(17) COLLATE pg_catalog."default" NOT NULL,
    ave_rssi numeric(5,2),
    CONSTRAINT clients_pkey1 PRIMARY KEY (clients_id)
)

This is my 'messages' table structure:

CREATE TABLE IF NOT EXISTS spot.messages
(
    message_id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
    "timestamp" timestamp without time zone,
    ap_mac character varying(30) COLLATE pg_catalog."default" NOT NULL,
    ap_id integer NOT NULL,
    ave_rssi numeric(5,2),
    airtime smallint,
    band smallint,
    number_of_clients integer,
    CONSTRAINT messages_pkey1 PRIMARY KEY (message_id)
)

This is my pivot table (client_message) structure:

CREATE TABLE IF NOT EXISTS spot.client_message
(
    id integer NOT NULL DEFAULT nextval('spot.client_message_id_seq'::regclass),
    clients_id integer NOT NULL,
    message_id integer NOT NULL,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    CONSTRAINT client_message_pkey PRIMARY KEY (clients_id, message_id)
)

When I go use PHP Artisan tinker, I can get an individual client record:

>>> $client = App\Client::find(232842935);
=> App\Client {#4792
     clients_id: 232842935,
     message_id: 10420248,
     mac: "F6:E6:0D:F3:1F:20",
     ave_rssi: "128.00",
   }

But when I attempt to get the message related to the client, I get an error:

>>> $client->message
=> null
>>> $client->messages
Illuminate\Database\QueryException with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "client_message" does not exist
LINE 1: ...ot_message_message_id" from "messages" inner join "client_me...
                                                             ^ (SQL: select "messages".*, "client_message"."client_clients_id" as "pivot_client_clients_id", "client_message"."message_message_id" as "pivot_message_message_id" from "messages" inner join "client_message" on "messages"."message_id" = "client_message"."message_message_id" where "client_message"."client_clients_id" = 232842935)'

Apparently, the pivot table is not visible to tinker and I don't know why.

Here is the 'Client' model class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
    use HasFactory;

    protected $primaryKey = 'clients_id';

    protected $table = 'clients';
    /**
     * Client shows up in many messages
     *
     * @return mixed
     */
    public function messages()
    {
        return $this->belongsToMany(Message::class);
    }
}

And here is the 'Message' model class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Message extends Model
{
    use HasFactory;

    protected $primaryKey = 'message_id';

    /**
     * Message has many clients - Many to Many
     *
     * @return mixed
     */
    public function clients()
    {
        return $this->belongsToMany(Client::class);
    }

    /**
     *
     *
     * @return mixed
     */
    public function controllerAp()
    {
        return $this->belongsTo(ControllerAp::class);
    }
}

Why can't I see the messages with Tinker?

0 likes
2 replies
Nakov's avatar

Make sure that whenever you add a change in your code, you restart the Tinker session.

Did you verified that the tables are created in your database as well?

denewey's avatar

Yes, I've been restarting Tinker with every change. And yes, the tables are created in the database. I can write queries off of them in my pgAdmin4.

Please or to participate in this conversation.