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

getInsertId() should be able to return negative ID's #336

Closed
joshualoehr opened this issue Sep 17, 2019 · 0 comments
Closed

getInsertId() should be able to return negative ID's #336

joshualoehr opened this issue Sep 17, 2019 · 0 comments

Comments

@joshualoehr
Copy link

joshualoehr commented Sep 17, 2019

I understand that using negative integers for PK's is generally considered bad practice, but that doesn't change the reality that our company's database has dozens of tables that use them. We no longer allow new schemas to have negative ID's, but we still have to support the legacy tables. We love everything about Dibi, but it's been very frustrating not being able to use getInsertId() for most of our queries because the code was written explicitly to throw an exception when encountering a negative ID (e.g. see: src/Dibi/Connection.php:318).

/**
 * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
 * @throws Exception
 */
public function getInsertId(string $sequence = null): int
{
	if (!$this->driver) {
		$this->connect();
	}
	$id = $this->driver->getInsertId($sequence);
	if ($id < 1) {
		throw new Exception('Cannot retrieve last generated ID.');
	}
	return $id;
}

At the very least, it would be great to have a configurable option allowing negative ID's.

I'd be happy to work on this feature myself if approved.

@dg dg closed this as completed in c1640c5 Sep 18, 2019
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

1 participant