laravel-doctrine / fluent

Fluent mapping driver for Doctrine2

Home Page:http://www.laraveldoctrine.org/docs/current/fluent

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

autoIncrement on non primary() field throws error

jaimesangcap opened this issue · comments

I'm trying to make a non primary key column to be autoIncrement but it's throwing error

PDOException: SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value

BUT when I'm removing the autoIncrement here

$builder->integer('taskNumber')
           ->unsigned()
           //->autoIncrement() // removing this will make it work
           ->treePathSource();

What is also happening is that the path is not getting the value taskNumber hence the path field has always empty when saved to database (maybe because of the autoIncrement not being used? but putting it causes error).

here is my mapping

public function map(Fluent $builder)
    {
        $builder->tree()->asMaterializedPath();
        $builder->entity()->setRepositoryClass(MaterializedPathRepository::class);

        $builder->table('task')
            ->charset('utf8mb4')
            ->collate('utf8mb4_unicode_ci');

        $builder->field('uuid', 'id')->primary();
        $builder->string('name')->length(191);
        $builder->text('description')->nullable();
        $builder->integer('status');
        $builder->carbonDateTime('deadline')->nullable();
        $builder->carbonDateTime('assignedAt')->nullable();
        $builder->manyToOne(Collaborator::class, 'assignee')->nullable();
        $builder->manyToOne(Collaborator::class, 'owner');

        $builder->oneToMany(Task::class, 'subTasks')->mappedBy('parent');
        $builder->manyToOne(Task::class, 'parent')
            ->cascadePersist()
            ->nullable()
            ->inversedBy('subTasks')
            ->treeParent();

        $builder->integer('taskNumber')
            ->unsigned()
            ->autoIncrement()
            ->treePathSource();

        $builder->string('path')
            ->treePath('/')
            ->appendId(false)
            ->startsWithSeparator()
            ->endsWithSeparator(false);

        $builder->integer('level')
            ->unsigned()
            ->treeLevel();

        $builder->timestamps();
    }

Model Factory

$factory->define(Task::class, function(Faker\Generator $faker, $attributes) {
    return [
        'id' => Uuid::uuid4(),
        'name' => $faker->sentences(1, $asText = true),
        'deadline' => Carbon::now()->addDays(1),
        'status' => TaskStatus::PENDING,
        'assignedAt' => Carbon::now(),
        'assignee' => $attributes['assignee'] ?? entity(Collaborator::class)->create(),
        'owner' => $attributes['owner'] ?? entity(Collaborator::class)->create(),
    ];
});

$factory->defineAs(Task::class, 'today', function(Faker\Generator $faker, $attributes) {
    $now = Carbon::now();

    return [
        'id' => Uuid::uuid4(),
        'name' => $faker->sentences(1, $asText = true),
        'assignedAt' => $now,
        'deadline' => $now->addDays(1),
        'status' => TaskStatus::PENDING,
        'assignee' => $attributes['assignee'] ?? entity(Collaborator::class)->create(),
        'owner' => $attributes['owner'] ?? entity(Collaborator::class)->create(),
    ];
});

You can't use auto increments on non-primary fields: this is a Doctrine limitation, not Fluent's.
As for your tree problem, I really can't help you with the autoIncrement issue in the middle of it. Fluent only bridges between LaravelDoctrine and Gedmo anyways, so this would not be the place to debug the extension itself.

If you find out that Fluent is mapping your tree incorrectly, please create a new issue.

Oops, thanks for pointing that out. I've been using doctrine for quite a while now and all I thought was that using the generated: auto option works on non-primary key since I'm not getting errors :)

The tree works after providing faker generated value on the taskNumber.

Thanks!