Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When creating a resource file from the db with a bigint key, the data type is mistakenly set to string and incrementing is set to false in the model. #214

Open
tcowin opened this issue Jan 8, 2025 · 1 comment

Comments

@tcowin
Copy link

tcowin commented Jan 8, 2025

This is a wonderful package and I've used it with several projects over the years (Many Thanks!), but have been modifying created models to correct this issue for a while, and I wanted to try to resolve this issue...

Environment:

  • Laravel-Code-Generator Version: 3.0.2
  • Laravel Version: 11.36.1

Description:

If my PK is a bigint, the data type gets set to string as the ParserBase::getHtmlType also uses the eloquent_type_to_html_type map, but there's not a key 'bigInteger' with a capital I, so it defaults to string. What we end up with in the model is this:

    /**
    * The database primary key value.
    *
    * @var string
    */
    protected $primaryKey = 'id';
    protected $keyType = 'string';
    public $incrementing = false;

When, ideally, no code would get added to the model as the default for PrimaryKey is id.

If I add a line to the map that accounts for this key like so 'bigInteger' => 'bigInteger', it works correctly, but I don't know if that is ideal as a fix. It seems there could be other issues with case sensitivity as you have other values in this map that are mixed case. Not sure if a strtolower on the key before the lookup in getHtmlType is optimal in all cases...?

Steps/Commands To Reproduce:

Run php artisan resource-file:from-database modelname on a database table with a bigint primary key.

Content Of The Resource-File:

{
    "fields": [
        {
            "name": "id",
            "labels": "Id",
            "html-type": "text",
            "css-class": "",
            "options": {},
            "html-value": null,
            "validation": "",
            "is-on-index": false,
            "is-on-show": false,
            "is-on-form": false,
            "data-type": "string",
            "data-type-params": [],
            "data-value": null,
            "is-index": false,
            "is-unique": false,
            "is-primary": true,
            "comment": null,
            "is-nullable": false,
            "is-header": false,
            "is-unsigned": true,
            "is-auto-increment": true,
            "is-inline-options": false,
            "is-date": false,
            "date-format": "",
            "cast-as": "",
            "placeholder": "Enter id here...",
            "delimiter": "; ",
            "range": [],
            "foreign-relation": null,
            "foreign-constraint": null,
            "on-store": null,
            "on-update": null,
            "api-key": "id",
            "is-api-visible": true,
            "api-description": "The id of the model."
        },
@MikeAlhayek
Copy link
Collaborator

@tcowin thanks for using this package and reporting this issue.

I don't think I have anytime soon to debug this. But you maybe able to test it out if you feel like it. You may be able to fix it and submit a PR for this.

I would start at

protected function getDataType($type, $columnType)

This is where we take in database types and convert them.

Try to debug it and see if you can find the issue. If not, at some point I'll try to spend time to look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants