Home » Generating Database Seed Files with ChatGPT [Article]

Generating Database Seed Files with ChatGPT [Article]

by Icecream
0 comment

Frameworks like Laravel and Ruby on Rails remove numerous selections that you’d in any other case be pressured to make when constructing net functions. Even so, quite a lot of time is spent on mundane duties quite than constructing the mental property that makes the appliance distinctive. One such process includes producing database seed information. In this publish I’ll present you the way you should utilize ChatGPT to generate each static and dynamic database seed information and shortly populate your improvement database with reasonable information.

What is a Database Seed File?

When constructing an online utility it is rather helpful to widespread your consumer interfaces with realistic-looking information. Some of this information is faked, that means as an example randomly generated consumer names and e-mail addresses, whereas different information is templated, that means it consists of static lists comparable to US states and Major League Baseball groups.

Once this information has been imported into your improvement database, it makes constructing out the assorted pages and dashboards way more reasonable. For instance right here is an web page populated with pretend customers. This check information is imported into the event database utilizing a seed file. For occasion a Laravel-based seed file containing US states may appear like this:

<?php

namespace DatabaseSeeders;

use IlluminateDatabaseConsoleSeedsWithoutModelEvents;
use IlluminateDatabaseSeeder;

use AppModelsState;

class StateSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public perform run(): void
    {

        State::truncate();

        $states = [
            ["name" => "Alabama", "abbreviation" => "AL"],
            ["name" => "Alaska", "abbreviation" => "AK"],
            ["name" => "Arizona", "abbreviation" => "AZ"],
            ["name" => "Arkansas", "abbreviation" => "AR"],
            ["name" => "California", "abbreviation" => "CA"],
            ["name" => "Colorado", "abbreviation" => "CO"],
            ["name" => "Connecticut", "abbreviation" => "CT"],
            ["name" => "Delaware", "abbreviation" => "DE"],
            ["name" => "District of Columbia", "abbreviation" => "DC"],
            ["name" => "Florida", "abbreviation" => "FL"],
            ["name" => "Georgia", "abbreviation" => "GA"],
            ["name" => "Hawaii", "abbreviation" => "HI"],
            ["name" => "Idaho", "abbreviation" => "ID"],
            ["name" => "Illinois", "abbreviation" => "IL"],
            ["name" => "Indiana", "abbreviation" => "IN"],
            ["name" => "Iowa", "abbreviation" => "IA"],
            ["name" => "Kansas", "abbreviation" => "KS"],
            ["name" => "Kentucky", "abbreviation" => "KY"],
            ["name" => "Louisiana", "abbreviation" => "LA"],
            ["name" => "Maine", "abbreviation" => "ME"],
            ["name" => "Maryland", "abbreviation" => "MD"],
            ["name" => "Massachusetts", "abbreviation" => "MA"],
            ["name" => "Michigan", "abbreviation" => "MI"],
            ["name" => "Minnesota", "abbreviation" => "MN"],
            ["name" => "Mississippi", "abbreviation" => "MS"],
            ["name" => "Missouri", "abbreviation" => "MO"],
            ["name" => "Montana", "abbreviation" => "MT"],
            ["name" => "Nebraska", "abbreviation" => "NE"],
            ["name" => "Nevada", "abbreviation" => "NV"],
            ["name" => "New Hampshire", "abbreviation" => "NH"],
            ["name" => "New Jersey", "abbreviation" => "NJ"],
            ["name" => "New Mexico", "abbreviation" => "NM"],
            ["name" => "New York", "abbreviation" => "NY"],
            ["name" => "North Carolina", "abbreviation" => "NC"],
            ["name" => "North Dakota", "abbreviation" => "ND"],
            ["name" => "Ohio", "abbreviation" => "OH"],
            ["name" => "Oklahoma", "abbreviation" => "OK"],
            ["name" => "Oregon", "abbreviation" => "OR"],
            ["name" => "Pennsylvania", "abbreviation" => "PA"],
            ["name" => "Rhode Island", "abbreviation" => "RI"],
            ["name" => "South Carolina", "abbreviation" => "SC"],
            ["name" => "South Dakota", "abbreviation" => "SD"],
            ["name" => "Tennessee", "abbreviation" => "TN"],
            ["name" => "Texas", "abbreviation" => "TX"],
            ["name" => "Utah", "abbreviation" => "UT"],
            ["name" => "Vermont", "abbreviation" => "VT"],
            ["name" => "Virginia", "abbreviation" => "VA"],
            ["name" => "Washington", "abbreviation" => "WA"],
            ["name" => "West Virginia", "abbreviation" => "WV"],
            ["name" => "Wisconsin", "abbreviation" => "WI"],
            ["name" => "Wyoming", "abbreviation" => "WY"]
        ];

        DB::desk('states')->insert($states);
    }
}

Even if you happen to don’t know something about Laravel, the code ought to be pretty comprehensible. I created a multidimensional array consisting of every state title and its corresponding two character abbreviation.

Prior to instruments like ChatGPT and Copilot, if you happen to wished to create a seed file containing US States, you’d usually go to Google, seek for one thing like “US State names and abbreviations”, after which painstakingly copy and paste the consequence into an array. Over time, builders understandably grew to become uninterested in this kind of work and created weblog posts and GitHub gists containing generally used seed information. For occasion if you happen to seek for “laravel us states seed file” you’ll discover fairly just a few assets containing helpful seed information. But even it is a tedious course of, as a result of you might want to type by means of the outcomes, discover code that meets your wants, and probably modify it a bit additional to completely fit your utility. Surely there’s a higher approach?

Land Your Dream Data Analyst Job in 2023!

Learn to code with Treehouse Techdegree’s curated curriculum filled with real-world initiatives and alongside unimaginable pupil help. Build your portfolio. Get licensed. Land your dream job in tech. Sign up for a free, 7-day trial in the present day!

Start a Free Trial

treehouse-badge

Enter ChatGPT

Using ChatGPT you’ll be able to generate these seed information utilizing a easy immediate. Let’s head over to https://chat.openai.com/ and create the identical seed file as I offered a second in the past. I’ll use the next immediate:

Please generate a Laravel seed file containing the 2 digit 
abbreviation and and title of every US state. Just present the 
code with out rationalization.

ChatGPT responded with:

<?php

use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;

class StatesDeskSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public perform run()
    {
        $states = [
            ['abbreviation' => 'AL', 'name' => 'Alabama'],
            ['abbreviation' => 'AK', 'name' => 'Alaska'],
            ['abbreviation' => 'AZ', 'name' => 'Arizona'],
            ['abbreviation' => 'AR', 'name' => 'Arkansas'],
            ['abbreviation' => 'CA', 'name' => 'California'],
            ['abbreviation' => 'CO', 'name' => 'Colorado'],
            ['abbreviation' => 'CT', 'name' => 'Connecticut'],
            ['abbreviation' => 'DE', 'name' => 'Delaware'],
            ['abbreviation' => 'FL', 'name' => 'Florida'],
            ['abbreviation' => 'GA', 'name' => 'Georgia'],
            ['abbreviation' => 'HI', 'name' => 'Hawaii'],
            ['abbreviation' => 'ID', 'name' => 'Idaho'],
            ['abbreviation' => 'IL', 'name' => 'Illinois'],
            ['abbreviation' => 'IN', 'name' => 'Indiana'],
            ['abbreviation' => 'IA', 'name' => 'Iowa'],
            ['abbreviation' => 'KS', 'name' => 'Kansas'],
            ['abbreviation' => 'KY', 'name' => 'Kentucky'],
            ['abbreviation' => 'LA', 'name' => 'Louisiana'],
            ['abbreviation' => 'ME', 'name' => 'Maine'],
            ['abbreviation' => 'MD', 'name' => 'Maryland'],
            ['abbreviation' => 'MA', 'name' => 'Massachusetts'],
            ['abbreviation' => 'MI', 'name' => 'Michigan'],
            ['abbreviation' => 'MN', 'name' => 'Minnesota'],
            ['abbreviation' => 'MS', 'name' => 'Mississippi'],
            ['abbreviation' => 'MO', 'name' => 'Missouri'],
            ['abbreviation' => 'MT', 'name' => 'Montana'],
            ['abbreviation' => 'NE', 'name' => 'Nebraska'],
            ['abbreviation' => 'NV', 'name' => 'Nevada'],
            ['abbreviation' => 'NH', 'name' => 'New Hampshire'],
            ['abbreviation' => 'NJ', 'name' => 'New Jersey'],
            ['abbreviation' => 'NM', 'name' => 'New Mexico'],
            ['abbreviation' => 'NY', 'name' => 'New York'],
            ['abbreviation' => 'NC', 'name' => 'North Carolina'],
            ['abbreviation' => 'ND', 'name' => 'North Dakota'],
            ['abbreviation' => 'OH', 'name' => 'Ohio'],
            ['abbreviation' => 'OK', 'name' => 'Oklahoma'],
            ['abbreviation' => 'OR', 'name' => 'Oregon'],
            ['abbreviation' => 'PA', 'name' => 'Pennsylvania'],
            ['abbreviation' => 'RI', 'name' => 'Rhode Island'],
            ['abbreviation' => 'SC', 'name' => 'South Carolina'],
            ['abbreviation' => 'SD', 'name' => 'South Dakota'],
            ['abbreviation' => 'TN', 'name' => 'Tennessee'],
            ['abbreviation' => 'TX', 'name' => 'Texas'],
            ['abbreviation' => 'UT', 'name' => 'Ut

Note how the output abruptly stops prior to finishing the output. This is because we’ve hit ChatGPT’s currently imposed 3,000 character limit. This restriction will almost certainly change in the future so you might not run into this issue, however if so you can use this prompt to coerce ChatGPT into gracefully continuing:

continue writing the code in last response

ChatGPT will pick right up where it left off, finishing the snippet:

            ['abbreviation' => 'UT', 'name' => 'Utah'],
            ['abbreviation' => 'VT', 'name' => 'Vermont'],
            ['abbreviation' => 'VA', 'name' => 'Virginia'],
            ['abbreviation' => 'WA', 'name' => 'Washington'],
            ['abbreviation' => 'WV', 'name' => 'West Virginia'],
            ['abbreviation' => 'WI', 'name' => 'Wisconsin'],
            ['abbreviation' => 'WY', 'name' => 'Wyoming'],
        ];

        DB::desk('states')->insert($states);
    }
}

From right here you’ll be able to copy each snippets into your seed file and proceed as desired.

Refining ChatGPT Code

Suppose you realized you made a mistake instructing ChatGPT to make use of abbreviation and title attributes for the seed entries, and as a substitute meant to make use of abbr for the abbreviation. Rather than ask ChatGPT to regenerate the enter seed file you’ll be able to say:

Please modify the $states array to make use of abbr as the important thing as a substitute of abbreviation. Return solely the states array and never your entire seed file.

ChatGPT returns:

$states = [
    ['abbr' => 'AL', 'name' => 'Alabama'],
    ['abbr' => 'AK', 'name' => 'Alaska'],
    ['abbr' => 'AZ', 'name' => 'Arizona'],
    ['abbr' => 'AR', 'name' => 'Arkansas'],
    ['abbr' => 'CA', 'name' => 'California'],
    ['abbr' => 'CO', 'name' => 'Colorado'],
    ['abbr' => 'CT', 'name' => 'Connecticut'],
    ['abbr' => 'DE', 'name' => 'Delaware'],
    ['abbr' => 'FL', 'name' => 'Florida'],
    ...

Generating Dynamic Seed Data with ChatGPT

You also can use ChatGPT to generate seed information dynamically. Most programming languages help this means by means of a third-party open supply dependency. See the Teacher’s notes for just a few examples. In the case of PHP that third-party dependency is known as Faker. ChatGPT occurs to grasp Faker and so we will instruct it to make use of Faker to generate 25 customers:

You are a Laravel programmer. Create a seed file that makes use of the Faker library to seed 25 customers right into a desk named customers. Each consumer ought to include a first_name, last_name, and e-mail subject.

ChatGPT will reply with:

<?php

use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
use FakerManufacturing facility as Faker;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public perform run()
    {
        $faker = Faker::create();

        for ($i = 0; $i < 25; $i++) {
            DB::desk('customers')->insert([
                'first_name' => $faker->firstName,
                'last_name' => $faker->lastName,
                'email' => $faker->unique()->safeEmail,
            ]);
        }
    }
}

If after producing the code you’re reminded that your customers desk doesn’t break the customers’ names into separate first and final title fields, you’ll be able to ask ChatGPT to revise the code:

Please revise your code so every consumer has a single subject for the primary and 
final title as a substitute of separate fields first_name and last_name.

ChatGPT will reply with the next:

<?php

use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
use FakerManufacturing facility as Faker;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public perform run()
    {
        $faker = Faker::create();

        for ($i = 0; $i < 25; $i++) {
            $fullName = $faker->title;
            $e-mail = $faker->distinctive()->safeEmail;

            DB::desk('customers')->insert([
                'name' => $fullName,
                'email' => $email,
            ]);
        }
    }
}

In this response I don’t like how ChatGPT first declared $fullName and $e-mail variables, as a result of the code shall be a tad cleaner if we simply instantiate the title and safeEmail strategies instantly contained in the insert technique. Let’s ask it to revise the code:

Please revise the code to name $faker object strategies inside 
the insert technique as a substitute of first instantiating variables for the title and e-mail.

Its response:

<?php

use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
use FakerManufacturing facility as Faker;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public perform run()
    {
        $faker = Faker::create();

        for ($i = 0; $i < 25; $i++) {
            DB::desk('customers')->insert([
                'name' => $faker->name,
                'email' => $faker->unique()->safeEmail,
            ]);
        }
    }
}

Great!

Conclusion

Tools like ChatGPT and GitHub Copilot can actually supercharge your productiveness by serving to you to shortly knock out tedious code and information units that in any other case would have required a good bit of Googling, copying, pasting, and reformatting. If you discovered this weblog publish helpful I invite you to join a free seven day Treehouse subscription to be taught extra about ChatGPT, AI, LLMs, information science, and extra!

You may also like

Leave a Comment