Skip to content

UUID used as a primary key causes panic when loading #1381

Closed
@korECM

Description

@korECM

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

v4.16.2

What is your database and version (eg. Postgresql 10)

MySQL 8.0

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

CREATE TABLE IF NOT EXISTS `events`
(
    `id`              BINARY(16)                         NOT NULL PRIMARY KEY,
    `name`            VARCHAR(255)                       NOT NULL,
    `insert_dt`       DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
    `update_dt`       DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
)
    ENGINE = InnoDB
    DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

CREATE TABLE IF NOT EXISTS `schedules`
(
    `id`                  BINARY(16)                         NOT NULL PRIMARY KEY,
    `event_id`            BINARY(16)                         NOT NULL,
    `insert_dt`           DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
    `update_dt`           DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,

    CONSTRAINT `schedules_event_id_fk` FOREIGN KEY (`event_id`) REFERENCES `events` (`id`) ON DELETE CASCADE,
)
    ENGINE = InnoDB
    DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

Further information. What did you do, what did you expect?

I am using UUID as a PK, and the generated code is as follows:

type Event struct {
	ID             []byte    `boil:"id" json:"id" toml:"id" yaml:"id"`
	Name           string    `boil:"name" json:"name" toml:"name" yaml:"name"`
	InsertDT       time.Time `boil:"insert_dt" json:"insert_dt" toml:"insert_dt" yaml:"insert_dt"`
	UpdateDT       time.Time `boil:"update_dt" json:"update_dt" toml:"update_dt" yaml:"update_dt"`

	R *eventR `boil:"-" json:"-" toml:"-" yaml:"-"`
	L eventL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

As you can see in the code above, the ID column uses the []byte type.

func (eventL) LoadSchedules(ctx context.Context, e boil.ContextExecutor, singular bool, maybeEvent interface{}, mods queries.Applicator) error {
	var slice []*Event
	var object *Event

	// ...

	args := make(map[interface{}]struct{})
	if singular {
		if object.R == nil {
			object.R = &eventR{}
		}
		args[object.ID] = struct{}{}
	} else {
		for _, obj := range slice {
			if obj.R == nil {
				obj.R = &eventR{}
			}
			args[obj.ID] = struct{}{}
		}
	}

	if len(args) == 0 {
		return nil
	}

	argsSlice := make([]interface{}, len(args))
	i := 0
	for arg := range args {
		argsSlice[i] = arg
		i++
	}

	// ...
	return nil
}

However, this leads to a problem in the code as follows:

		args[object.ID] = struct{}{}

Here, the type of the ID value is []byte, but a slice in Golang is not hashable. Therefore, the following error occurs:

	panic: runtime error: hash of unhashable type []uint8

#1337

This issue is suspected to have arisen from the above PR. If you use version v4.15.0, this problem does not occur. It's necessary to solve the performance issue with Load, but it would be good if it could be used without any problems even when using UUID as a PK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions