Skip to content

Auto increment ID values #10

@clphillips

Description

@clphillips

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
    ]
];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions