PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB » Bug #287

nextId() uses wrong sequence

Details

Submitted2003-11-23 23:07 UTC
Fromieure at websprockets dot com
Assigneddanielc
StatusBogus
PackageDB
PHP VersionIrrelevant
OSLinux
Roadmaps(Not assigned)

Comments

[2003-11-23 23:07 UTC] ieure at websprockets dot com

Description:
------------
Calling nextId (with pgsql) uses a different sequence than what I pass
in $seq_name.

Reproduce code:
---------------
Table def:
CREATE TABLE "users" (
"username" text NOT NULL,
"password" text NOT NULL,
"id" serial
);

(this autocreates 'users_id_seq')

Then:

$id = $DB->nextId('users_id_seq');

Expected result:
----------------
nextId() returns the next value from 'users_id_seq,' per the
documentation.

http://pear.php.net/manual/en/package.database.db.db-common.nextid.php
"string $seq_name - name of the sequence"

Actual result:
--------------
nextId() creates 'users_id_seq_seq' and returns the next value from
that. If the database was seeded prior to using DB, the insert will fail
due to duplicate IDs.

getSequenceName() (in DB/common.php) seems to be the
cuplrit, as it returns 'users_id_seq_seq' when called from nextId().

[2003-12-18 16:50 UTC] chagenbu at php dot net

This is now nextId() has always behaved, and changing it would break a lot of applications. Is there a reason you can't just call $db->nextId('users_id')?

[2004-01-15 19:28 UTC] ieure at websprockets dot com

The bug isn't bogus, the problem is real. The fact that DB
has had the bug for a long time and code now depends on it
is irrelevant.

At the very least, the documentation should be updated to
indicate that it does /not/ use the sequence you tell it
to, and perhaps a warning not to let nextId create
sequences for you.

Long-term, it should be fixed (perhaps in 2.0), or if we
absolutely must keep it's (utterly broken) behavior, a
replacement function which works correctly and has a
different name should be added.

[2004-01-15 21:01 UTC] ieure at websprockets dot com

In response to Chuck's question, no, there is no reason I
can't simply use 'tablename_fieldname' instead of (the
correct) 'tablename_fieldname_seq.' Indeed, it's what I
/had/ to do in order to work around this problem.

Regardless, the documentation does not say "name of the
sequence, with _seq omitted," nor does it say "sequence
name will be converted to the format specified in the
'seqname_format' option. It says "name of the sequence,"
and it does not use the name you give it.

It's not obvious that this is the behavior until you start
having problems with your database and have to track down
what's going wrong.

Setting seqname_format to '%s' makes nextId() use the
sequence I pass it. This still should be documented
somewhere.

[2004-01-16 07:55 UTC] ieure at websprockets dot com

All this discussion is quite irrelevant, imo. The function
does not do what the documentation claims. Either the
documentation needs to be updated to tell what the function
does, or the function needs to be changed to do what the
documentation says. I'll be happy to provide a patch for
either one.

We could argue about use cases forever, but the simple fact
is that the function doesn't do what the docs say.

[2004-11-26 13:13 UTC] cece at syrup dot hu

Why not use

$id = $DB->nextId('users_id');

instead?