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

Auto increment ID values #10

Open
clphillips opened this issue Apr 17, 2015 · 1 comment
Open

Auto increment ID values #10

clphillips opened this issue Apr 17, 2015 · 1 comment

Comments

@clphillips
Copy link
Contributor

Random IDs might be okay for testing, but seeding generally requires that table IDs increment in the order they were inserted.

This can be accomplished by assigning a count to each item in a table fixture and referencing the count when inserting.

// apples.php
return [
    // ID: 1
    'Granny-Smith' => [
        'color' => 'green',
        'description' => 'Tart'
    ],
    // ID: 2
    'Red Delicious' => [
        'color' => 'red',
        'description' => 'Slightly Tart'
    ]
];

So when we insert a foreign key:

// apple_sauces.php
return [
    // ID: 1
    'Treetop' => [
        'apple_id' => 'Red Delicious' // foreign key ID 1
    ],
    // ID: 2
    'Kirkland' => [
        'apple_id' => 'Red Delicious' // foreign key ID 1
    ]
];

Obviously requires preprocessing all fixtures.

Here's an alternative (allow integer values to be assigned as foreign keys):

// apple_sauces.php
return [
    // ID: 1
    'Treetop' => [
        'apple_id' => 1
    ],
    // ID: 2
    'Kirkland' => [
        'apple_id' => 1
    ]
];
@clphillips
Copy link
Contributor Author

Note: This can be accomplished if PR #12 is merged and we allow PDODriver::generateKey() and KeyGeneratorInterface::generateKey() to accept a second (optional) parameter containing the table name.

Use table name to lookup fixture file, then value to lookup numeric index. If value is int, simply return 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

1 participant