laravel-shift / blueprint

A code generation tool for Laravel developers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot set primary key, column always sticking to integer column

SiL3NC3 opened this issue · comments

  • Laravel Version: v9.52.5
  • PHP Version: v8.0.16
  • Blueprint Version: #.#.#
  • Platform: Windows

Issue:

I'm playing around with blueprint and trying to recreate the sessions table.
Within Session table, "last_activity" will always get the primary key assigned.

Also with password resets table. How to prevent creation of automatic id column?

But the column "id" should be the primary key, even it is a string (like in the documentation)

image

https://laravel.com/docs/10.x/session#database

But an error occurred:
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined")

draft.yaml:

  User:
    id: bigIncrements
    user_member_id: id foreign
    name: string:255
    email: string:255 unique index
    email_verified_at: timestamp nullable
    password: string:255
    remember_token: string:100
    last_login: timestamp nullable
    failed_logins: tinyInteger
    disabled: boolean
    timestamps: true
    softDeletes: true

  Session:
    id: string primary
    user_id: id foreign
    ip_addess: string:45
    user_agent: text
    payload: longText
    last_activity: integer:10
    timestamps: false
    relationships:
      belongsTo: User

  PasswordReset:
    id: none  ???
    email: string primary
    token: string
    created_at: timestamp nullable
    timestamps: false

What does Blueprint generate?

        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_member_id')->constrained();
            $table->string('name', 255);
            $table->string('email', 255)->unique()->index();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password', 255);
            $table->string('remember_token', 100);
            $table->timestamp('last_login')->nullable();
            $table->tinyInteger('failed_logins');
            $table->boolean('disabled');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id');
            $table->foreignId('user_id')->constrained();
            $table->string('ip_addess', 45);
            $table->text('user_agent');
            $table->longText('payload');
            $table->integer('last_activity', 10);
        });

        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('id');
            $table->string('email')->primary();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });

@SiL3NC3 For string (uuid) column with the name "id", use id: uuid primary.

The string manipulation is happening in MigrationGenerator.php.

If I remember correctly, it should generate $table->uuid('id')->primary();.

@ghostwriter

Still getting the above error (multiple primary keys defined), with "last_activity" as primary key gets created first...

If I uncomment the column "last_activity" the string primary column is being created properly.
Blueprint seems strongly expecting, that any integer type column is the primary key...

Is there a way to prevent "id" column is being created (if not needed, like in PasswortResets table or does a flag exists to prevent the automatic choosing of primary keys (as like Session table with the only int typed column strongly gets the primary key?

Got it working, figured out the problem:
When I remove the digit setting...

last_activity: integer:10 >> last_activity: integer

Correct working for the base tables after a new session-based laravel setup I ended up with this draft.yaml

models:
  FailedJob:
    id: bigIncrements
    uuid: string:255 unique
    connection: text
    queue: text
    payload: longText
    exception: longText
    failed_at: timestamp useCurrent
    timestamps: false

  User:
    id: bigIncrements
    name: string:255
    email: string:255 unique index
    email_verified_at: timestamp nullable
    password: string:255
    remember_token: string:100 nullable
    timestamps: true

  PasswordReset:
    id: false
    email: string primary
    token: string
    created_at: timestamp nullable
    timestamps: false

  Session:
    id: string primary
    user_id: id foreign nullable
    ip_addess: string:45 nullable
    user_agent: text nullable
    payload: longText
    last_activity: integer index
    timestamps: false
    relationships:
      belongsTo: User

@SiL3NC3, yep, looks like you found it! 💪🏾

the syntax to "skip" generating the id column.

id: false

I'm not sure why the integer:10 is the issue. Setting the column should be supported. Just maybe not for integers.