Support for HasUuids trait #423
-
Hey, I am currently following a tutorial on your website and wanted to do a minor modification to the code, specifically, using UUIDs as primary key for the event instead of the regular This is my modification to the original code: // app/Models/Account
class Account extends Projection
{
use HasUuids;
protected $guarded = [];
public static function createWithAttributes(array $attributes): Account
{
$attributes['uuid'] = (string)Uuid::uuid4();
event(new AccountCreated($attributes));
return static::uuid($attributes['uuid']);
} use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountsTable extends Migration
{
public function up()
{
Schema::create('accounts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('uuid');
$table->string('name');
$table->integer('balance')->default(0);
$table->timestamps();
});
}
} This results in the following error: Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (Connection: mysql, SQL: insert into `accounts` (`name`, `uuid`, `updated_at`, `created_at`) values (Account Name, 625fc413-e62c-4fe6-9460-c9fa7e2f9e48, 2023-07-15 18:51:31, 2023-07-15 18:51:31)).
I can get rid of the error by setting the Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi! Is there a reason you have both an // app/Models/Account
class Account extends Projection
{
use HasUuids;
protected $guarded = [];
public static function createWithAttributes(array $attributes): Account
{
$attributes['id'] = (string) Uuid::uuid4();
event(new AccountCreated($attributes));
return static::find($attributes['uuid']);
} use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountsTable extends Migration
{
public function up()
{
Schema::create('accounts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->integer('balance')->default(0);
$table->timestamps();
});
}
} |
Beta Was this translation helpful? Give feedback.
Hi!
Is there a reason you have both an
id
anduuid
column? Here's how I'd set it up: