---
title: I design, AI code
url: https://calvin.my/posts/i-design-ai-code
published: 2024-06-28
updated: 2026-09-17
category: AI
tags:
- Copilot
- Anthropic
- Rails
summary: The post explores using an AI assistant to convert database diagrams into Rails migration plans for a blog application. After submitting an initial schema, the author finds that the generated output largely captures tables, fields, indexes, and relationships but misses one association and uses an undesirable table name. Follow-up prompts correct the omission and produce migrations for later diagram changes, including comments, category priority, and article metadata. The experience suggests AI can reduce repetitive schema implementation work, though results still require review.
---

# I design, AI code

I am always excited when it comes to designing database schemas with my colleagues. We draw on the whiteboard or collaborate via drawio to come out with a design that fits the application. However, turning the design into a long list of migration files is boring.

I will need to make sure the order of creation is correct, provides the relevant default values, and defines the correct data type, sizes, etc.

Now, how about I draw my design and get AI to do write all the migration files for me?

* * *

## Prepare a database design

First, I draw a simple database design for a blog. The main table is articles, it has an author (user), it belongs to a category and it has multiple tags. I purposely leave an ambiguous statement in the yellow box, and see what will happen.

![](https://camy-pub.s3.ap-southeast-1.amazonaws.com/45b8b281-2913-4b2c-befa-0f8159aba04f.jpg)

* * *

## Send it to Anthropic

Next, I send this image to Anthropic, together with the following prompt.

_Please turn this database design into rails migrations. By the way, I don't have exact idea on the yellow box yet, please do suggest something for me._

Here is the code it returns:

```ruby
class CreateUsers < ActiveRecord::Migration[6.1]
  def change
    create_table :users do |t|
      t.string :name, limit: 100
      t.string :password, limit: 255
      t.string :email, limit: 255
      t.string :phone, limit: 15

      t.timestamps
    end
    add_index :users, :email, unique: true
  end
end

class CreateArticles < ActiveRecord::Migration[6.1]
  def change
    create_table :articles do |t|
      t.string :title, limit: 100
      t.text :content
      t.string :slug, limit: 150
      t.string :status, limit: 1, default: 'a'
      t.references :user, foreign_key: true

      t.timestamps
    end
    add_index :articles, :slug, unique: true
  end
end

class CreateCategories < ActiveRecord::Migration[6.1]
  def change
    create_table :categories do |t|
      t.string :name, limit: 20, null: false

      t.timestamps
    end
    add_index :categories, :name, unique: true
  end
end

class CreateTags < ActiveRecord::Migration[6.1]
  def change
    create_table :tags do |t|
      t.string :name, limit: 20, null: false

      t.timestamps
    end
    add_index :tags, :name, unique: true
  end
end

class CreateArticlesTags < ActiveRecord::Migration[6.1]
  def change
    create_table :articles_tags, id: false do |t|
      t.references :article, foreign_key: true
      t.references :tag, foreign_key: true
    end
    add_index :articles_tags, [:article_id, :tag_id], unique: true
  end
end

class CreateArticleMeta < ActiveRecord::Migration[6.1]
  def change
    create_table :article_meta do |t|
      t.references :article, foreign_key: true
      t.integer :total_words
      t.integer :recommended_reading_time
      t.integer :views_count, default: 0
      t.string :meta_description, limit: 160
      t.string :meta_keywords, limit: 255

      t.timestamps
    end
  end
end
```

Everything seems correct, except:

1. It missed the relationship between article and category
2. It would be better if ArticleMeta table is in plural - ArticleMetas

* * *

## Fixing the mistakes

I get it to help me amend the missing relation with the following prompt.

_i think you missed the relation between article and category?_

It gives 2 choices - to edit if I have not run the migration yet, or to create a new migration. I choose the latter.

```ruby
class AddCategoryToArticles < ActiveRecord::Migration[6.1]
  def change
    add_reference :articles, :category, foreign_key: true
  end
end
```

* * *

## Adding more stuff...

Next, I decided to enhance my design, by adding a new table and modifying an existing table. I called it design v2.

![](https://camy-pub.s3.ap-southeast-1.amazonaws.com/8957bf9e-c7b6-49ed-8c65-f5a9bf88c7bd.jpg)

I send the new image together with the prompt below.

_thanks for your suggestion on the article meta. it is useful. on top of that, i also updated the diagram with some new changes. can you create the migration for the new changes?_

From the response, some new migrations are generated.

```ruby
class CreateComments < ActiveRecord::Migration[6.1]
  def change
    create_table :comments do |t|
      t.string :message, limit: 255
      t.references :article, foreign_key: true

      t.timestamps
    end
  end
end

class AddPriorityToCategories < ActiveRecord::Migration[6.1]
  def change
    add_column :categories, :priority, :integer
  end
end

class CreateArticleMetas < ActiveRecord::Migration[6.1]
  def change
    create_table :article_metas do |t|
      t.references :article, foreign_key: true
      t.integer :total_words
      t.integer :recommended_reading_time
      t.integer :views_count
      t.string :meta_description, limit: 160
      t.string :meta_keywords, limit: 255

      t.timestamps
    end
  end
end
```

It identified that:

✅ There is a new comments table

✅ A priority field is added to the categories table&nbsp;

✅ I have reflected the article meta table into the design

* * *

And that is it. We just completed the database migrations with the help of AI. It has surprisingly high accuracy and is simple to use.
