May Sale! All accounts are 40% off this week.

mstdmstd's avatar

Why in Notificationtable inserted row has strange id ?

In laravel 11 / livewire 3 app I send email using parameter in via method :


class NewStaffUserCreatedNotification extends Notification
{
    use Queueable;

    protected User $staffUser;
    protected bool $isActive;
    protected string $generatedPassword;

    public function __construct(User $staffUser, bool $isActive = false, string $generatedPassword = '')
    {
        $this->staffUser = $staffUser;
        $this->isActive = $isActive;
        $this->generatedPassword = $generatedPassword;
    }

    public function via(object $notifiable): array
    {
        return ['database'];
    }
    ...

I got strange error :

[2024-07-12 10:56:21] local.INFO: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'id' at row 1 (Connection: mysql, SQL: insert into `notifications` (`id`, `type`, `data`, `read_at`, `notifiable_id`, `notifiable_type`, `updated_at`, `created_at`) values (2fad7130-e97c-46d6-9791-cdff36ff548d, App\Notifications\NewStaffUserCreatedNotification, {"data":{"staffUser":{"name":"Laura Atkins","email":"[email protected]","slug":"laura-atkins","notes":"<p>f<\/p>","status":"N","membership_mark":"M","phone":"+1 (663) 838-9799","website":"https:\/\/www.rapesy.mobi","activated_at":null,"avatar":"public\/avatars\/avatar-50\/6wxK2Q4gL6OrsE9m8LIXepgCiVWbPBhNyXBHwhIO.jpg","id":50}}}, ?, 50, App\Models\User, 2024-07-12 10:56:21, 2024-07-12 10:56:21))

Value "2fad7130-e97c-46d6-9791-cdff36ff548d" really seems invalid for field defined :


    CREATE TABLE `notifications` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,

It seems I did not modify notifications anyway.

This notification is called from listener:
    public function handle(NewStaffUserCreatedEvent $event): void
    {
        // I check that vaiid parameters are passed
        \Log::info(varDump($event, ' -1 NewStaffUserCreatedListener $event::'));
        $event->staffUser->notify(new NewStaffUserCreatedNotification(staffUser: $event->staffUser, isActive: $event->isActive,
        generatedPassword: $event->generatedPassword));

Any ideas why I got such error ? What have I to check ?

0 likes
13 replies
Snapey's avatar

clearly you have modified the notifications table

mstdmstd's avatar

@Snapey I have found in file database/migrations/2024_04_21_161455_create_notifications_table.php :

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;


class CreateNotificationsTable extends Migration
{
    public function up()
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('type');
            $table->morphs('notifiable');
            $table->text('data');
            $table->timestamp('read_at')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'read_at']);
        });
    }

    public function down()
    {
        Schema::dropIfExists('notifications');
    }
}

Not any uuid...

Snapey's avatar

@mstdmstd For Laravel 11, the command php artisan make:notifications-table publishes migration like;

    public function up(): void
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('type');
            $table->morphs('notifiable');
            $table->text('data');
            $table->timestamp('read_at')->nullable();
            $table->timestamps();
        });
    }

primary key is type uuid

Perhaps your database originates from an earlier version?

1 like
Snapey's avatar

As far as I can see, the notifications have never used auto increment ID and were string up until Oct 2016 when they were changed to uuid.

Perhaps your own repo can tell you the history of your notifications table?

Snapey's avatar

@mstdmstd also, your notifications table has a user_id column which is not standard and also not nullable.

Yet, your notification tries to insert a row without a user_id (as expected) so if your insert had not failed because of the ID column, it would have also failed as no user_id was given.

Seems maybe your database table exists for some other reason.

mstdmstd's avatar

Also I foiund a model app/Models/Notification.php :

namespace App\Models;

use App\Enums\ConfigValueEnum;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\MassPrunable;

class Notification extends Model
{
    use MassPrunable;

    /* old Models would be removed automatically */
    public function prunable(): Builder
    {
        $autoDeleteModelsAfterDays = ConfigValueEnum::get(ConfigValueEnum::AUTO_DELETE_MODELS_AFTER_DAYS);
        return static::whereNotNull('read_at')
            ->where('read_at', '<=', now()->subDays($autoDeleteModelsAfterDays));
    }
}

Can be reason that some default declarations missing ?

JussiMannisto's avatar

Just to make it simple, here's what's wrong:

  1. Laravel has a notification system that uses a table called notifications. It has a UUID primary key.
  2. You have a custom model named Notification with a table named notifications. It has an integer primary key.

1 and 2 are in conflict and you get errors because of that.

1 like
mstdmstd's avatar

@JussiMannisto Please, write which steps have I to take ? delete wrong table definition in migration ? Reinit notification system ? In which way ?

JussiMannisto's avatar

@mstdmstd Why do you have a customized version of the notifications table? Who customized it? Maybe they know why it was done like that.

I can't help because I don't know what your app is trying to do. But if it's supposed to work with Laravel's native notifications, then it shouldn't have an integer primary key but a UUID.

mstdmstd's avatar

@JussiMannisto a)I am a single developer of the project 2) Not sure, but that is possible, that missing notifications table in my db I found and copypasted it from inet 3) So I would like to reinit notifications system. I did not find has it any notifications::install or similar ?

Please or to participate in this conversation.