Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Can't simulate insertion of rows

posted by audvare 8 years ago

Using this schema:

DROP TABLE IF EXISTS poll_basic_results;
CREATE TABLE poll_basic_results (
  rid INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
  pid INT UNSIGNED NOT NULL DEFAULT 0,
  uid INT UNSIGNED NOT NULL DEFAULT 0,
  answer INT UNSIGNED NOT NULL DEFAULT 0,
  timestamp INT UNSIGNED NOT NULL DEFAULT 0,
  code VARCHAR(32) NOT NULL DEFAULT '',
  zip VARCHAR(16) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE UNIQUE INDEX rid_pid ON poll_basic_results (rid, pid);

This code is supposed to populate the table, with 1000 rows of random choices made.

      $time = time();
      $pid = arg(2, 'int');
      $poll = new Poll($pid);
      $max = 2;
      if ($poll->getAnswer3() != '') {
        $max = 3;
      }
      if ($poll->getAnswer4() != '') {
        $max = 4;
      }
      
      for ($i = 0; $i < 1000; $i++) {
        $result = new PollBasicResult;
        $result->setPid($pid);
        $result->setAnswer(fCryptography::random(1, $max));
        $result->setTimestamp($time);
        $result->store();
      }
      
      Base_Router::route('poll/'.$pid);
    }
    catch (fNotFoundException $e) {
      Base_Router::route('404');
    }
    catch (fValidationException $e) {
      die($e->getMessage());
    }

But I get this error:

#!text/html
Rid, Pid: The values specified must be a unique combination, however the specified combination already exists

Yet, I can insert new rows manually with the same Pid, because Rid is auto-incrementing:

#!text/html
mysql> INSERT INTO poll_basic_results (pid, uid, answer, timestamp) VALUES(40, 0, 1, UNIX_TIMESTAMP());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO poll_basic_results (pid, uid, answer, timestamp) VALUES(40, 0, 1, UNIX_TIMESTAMP());
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO poll_basic_results (pid, uid, answer, timestamp) VALUES(40, 0, 1, UNIX_TIMESTAMP());
Query OK, 1 row affected (0.01 sec)

No problem over and over again. Why is the code attempting to assign Rid? Even passing NULL (as phpMyAdmin does) doesn't do anything ($result->setRid(NULL);).

Enabled debug. What I don't understand is why the select is made on the second time the loop is about to go (maybe at the setPid() line).

#!text/html
Query time was 0.0001220703125 seconds for:
SHOW CREATE TABLE "poll_basic_results"
Query time was 8.2969665527344E-5 seconds for:
BEGIN
Query time was 0.00014495849609375 seconds for:
SELECT "rid" FROM "poll_basic_results" WHERE "poll_basic_results"."rid" IS NULL AND "poll_basic_results"."pid" = 41
Query time was 0.0001380443572998 seconds for:
INSERT INTO "poll_basic_results" ("pid", "uid", "answer", "timestamp", "code", "zip") VALUES (41, 0, 2, 1302739078, '', '')
Query time was 0.00058913230895996 seconds for:
COMMIT
Query time was 8.6069107055664E-5 seconds for:
BEGIN
Query time was 0.00016403198242188 seconds for:
SELECT "rid" FROM "poll_basic_results" WHERE "poll_basic_results"."rid" IS NULL AND "poll_basic_results"."pid" = 41
Query time was 8.5115432739258E-5 seconds for:
ROLLBACK
The following problems were found:

Rid, Pid: The values specified must be a unique combination, however the specified combination already exists
Total query time: 0.025109529495239 seconds

Then I removed the UNIQUE KEY, which allows the loop to run but for some reason it makes a lot more than 1000 rows. It runs until PHP runs out of memory (and I only allow up to 128 MB). Any reason for this? Is the random() function using more memory as time goes by?

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) in /home/tatsh/dev/poluza/sutra/trunk/Libraries/Flourish/fDatabase.php on line 2491

posted by audvare 8 years ago

Can you paste the output of the following?

fCore::expose(fORMDatabase::retrieve()->getColumnInfo('poll_basic_results'));

It almost seems as if fSchema is not detecting your database schema properly.

posted by wbond 8 years ago

Fatal error: Call to undefined method fDatabase::getColumnInfo() Couldn't figure out what you meant if that was mistaken.

posted by audvare 8 years ago

Sorry, that should have been:

fCore::expose(fORMSchema::retrieve()->getColumnInfo('poll_basic_results'));
posted by wbond 8 years ago

Did you ever get this to work?

posted by wbond 8 years ago

Yes. I didn't add the unique key so I don't have the expose() output for you but I did realise I was calling the wrong function which resulted in a redirect loop, the reason why the loop was 'resetting' back to 0 after it hit 1000. Maybe a kind of bug on how I route a path (?q of the URL string but rewritten without the ?q=) in my system but I'm not sure there's really a way around it (yet).

posted by audvare 8 years ago