top of page
Writer's picturePaul Found

Creating a Fake Database Generator with Python

Introduction

As a data engineer, I often find myself needing to test data pipelines or experiment with new ideas using databases that reflect real-world complexities. Recently, I found myself pondering a particular challenge: how could I create a database filled with fake data that not only mimicked the structure of a real-world database but also respected the relationships between the tables? This thought process led me to the idea of creating a Fake Database Generator with Python. My goal was to build a tool that could automate the generation of a fully functional database, complete with foreign key constraints and relational integrity, all based on an existing database schema.


This idea got me thinking: if I could define the schema upfront—using a format as simple as JSON—could I then automate the process of generating a fully functional database filled with realistic data? This would save me countless hours of manually crafting tables, inserting data, and ensuring everything connects correctly. It would also make it easy to replicate and adapt for different projects.


Driven by this idea, I created the fake-database-generator. This Python-based tool takes a JSON schema as input then dynamically generates the tables and relationships, and populates them with realistic, fake data using the powerful Faker library.


In this post, I’ll walk you through the thinking behind the tool, how it should work, and how you can use it to define and fill up databases quickly.


The Problem with Manually Creating Test Databases

When you're developing data-driven applications or pipelines, having a reliable test environment is essential. However, manually creating and populating a database that accurately reflects a production environment can be a major hassle. You need data that not only looks realistic but also maintains the relationships and constraints that exist in a real-world scenario.

For many data engineers, the process usually involves the following pain points:


  1. Time-Consuming Setup: Crafting tables, defining relationships, and inserting data by hand takes significant time, especially when your schema involves multiple tables with complex relationships.


  2. Maintaining Data Integrity: Ensuring that the fake data respects the relationships between tables—such as foreign key constraints—is a tedious task. Without these relationships, the data might look correct, but it won’t behave like real-world data when queried or processed.


  3. Scalability Issues: Manually generating large datasets to test the performance of your ETL jobs or analytics processes is impractical. You need a way to automate this, so you can quickly spin up large, relational datasets on demand.


  4. Replicability and Adaptability: Once you have a working test database, replicating it across different environments or adapting it for slightly different schemas can be labour-intensive. The process doesn’t scale well when you need to iterate or make adjustments.


These challenges led me to ask: what if there was a way to automate this entire process? I wanted to create a tool that could dynamically generate a database with tables, relationships, and realistic data—all based on a schema I could define in advance. The goal was to move from manual data creation to a fully automated, scalable solution that would allow me to generate the test environments I needed, when I needed them.


This is what sparked the creation of the fake-database-generator. By leveraging the flexibility of JSON for schema definition and the power of Python’s Faker library for data generation, this tool addresses these common pain points, making the setup of realistic test databases quick, easy, and repeatable.


How It Works

With the problem clearly defined, let’s dive into how the fake-database-generator solves it. The key to this tool is its ability to dynamically create a relational database, populate it with realistic data, and maintain all the necessary relationships—all based on a simple JSON schema.


Here’s a breakdown of how it works:


1. Defining the Schema in JSON

The foundation of the fake-database-generator is the JSON schema. This schema acts as the blueprint for your database, defining each table, its fields, data types, and the relationships between tables. By using JSON, you can easily describe complex structures in a human-readable format.


A typical JSON schema looks like this:

[
	{
	"table_name": "Users",
	"fields": [
		{"name": "user_id", "type": "INTEGER", "primary_key": true},
		{"name": "first_name", "type": "TEXT", "fake_data":"first_name"},
		{"name": "last_name", "type": "TEXT", "fake_data": "last_name"},
		{"name": "email", "type": "TEXT", "fake_data": "email"}
	]
	},
	{
	"table_name": "Orders",
	"fields": [
		{"name": "order_id", "type": "INTEGER", "primary_key": true},	
		{"name": "user_id", "type": "INTEGER", "foreign_key": {"references": "Users(user_id)"}},
		{"name": "order_date", "type": "TEXT", "fake_data": "date_this_year"},
		{"name": "total_amount", "type": "REAL", "fake_data": "random_float"}
	]
	}
]

In this example:


  • The Users table is defined with fields for user_id, first_name, last_name, and email.


  • The Orders table includes a user_id field that references the Users table, creating a relationship between the two tables.


The schema can be as simple or as complex as you need, supporting multiple tables and intricate relationships.


2. Generating the Database

Once the JSON schema is defined, the fake-database-generator reads it and dynamically creates the database structure. Here’s what happens under the hood:


  • Table Creation: The tool iterates through the schema, creating each table in an SQLite database according to the specifications in the JSON file. Primary keys, data types, and foreign key constraints are all set up automatically.


  • Foreign Key Constraints: The generator ensures that relationships between tables are maintained by enforcing foreign key constraints. This means that every user_id in the Orders table must correspond to a valid user_id in the Users table, just as it would in a real-world database.


3. Populating the Database with Fake Data

With the structure in place, the generator then populates each table with realistic fake data. Here’s how:


  • Faker Library Integration: The generator leverages Python’s Faker library to create data that looks and behaves like real-world data. For example, first_name fields are populated with realistic first names, while email fields get properly formatted email addresses.


  • Relationship Integrity: When generating data for a table with foreign keys (e.g., Orders referencing Users), the generator first ensures that the referenced table (e.g., Users) is populated. It then assigns foreign key values that correspond to actual entries in the referenced table, maintaining relational integrity.


4. Retrieving and Using the Data

After the database is generated and populated, you can query it just like you would with any other relational database. The relationships are intact, meaning you can perform joins, filter data based on relationships, and use the database for testing, prototyping, or demonstrations.


For example, you could easily retrieve all orders placed by a specific user:

SELECT 
	Orders.order_id, 
	Orders.order_date, 
	Users.first_name, 
	Users.last_name
FROM 
	Orders
JOIN
	Users ON Orders.user_id = Users.user_id
WHERE 
	Users.email = 'john.doe@example.com';

The query will return meaningful, interconnected data that behaves just like it would in a real production environment.


5. Customizing and Extending the Tool

One of the great strengths of the fake-database-generator is its flexibility. You can easily modify the JSON schema to match different database structures, or extend the tool by adding new types of fake data. This makes it highly adaptable to various use cases, from simple prototypes to more complex testing environments.


Extending the Tool

The fake-database-generator is designed with flexibility in mind, making it easy to extend and customize for your specific needs. Here are a few key ways you can build on the tool’s foundation:


1. Adding New Data Types

If you need to generate specialized data, such as geographical coordinates or financial details, you can easily extend the tool by adding new data types to the generate_fake_value method. This allows you to tailor the fake data to better match the specific requirements of your projects.


2. Supporting Multiple Database Engines

While the tool currently uses SQLite, it can be adapted to support other database systems like PostgreSQL or MySQL. By abstracting the database layer or integrating with an ORM like SQLAlchemy, you can make the tool versatile enough to work with different database backends, broadening its applicability.


3. Enhancing Schema Validation

To ensure the JSON schema is correct and consistent, you can implement schema validation. This helps catch errors early, ensuring that the generated database structure is reliable and adheres to your expectations, especially when dealing with complex schemas.


4. Scaling for Performance Testing

For scenarios requiring large datasets, you can enhance the tool’s performance by implementing batch inserts or parallel processing. This enables the generation of massive datasets efficiently, making the tool suitable for stress-testing data pipelines and systems.


5. Integrating with CI/CD Pipelines

Integrate the tool into your CI/CD workflows by adding command-line options or scripting it to run automatically in testing pipelines. This ensures that every deployment has access to a consistent, freshly generated database for testing, improving the reliability of your development processes.


Conclusion

The fake-database-generator was born out of a desire to make life easier for data engineers by automating the tedious process of creating test databases. By allowing you to define your database schema in JSON and automatically generating realistic, relational data, this tool aims to save you time and effort while ensuring that your test environments are as close to reality as possible.


I hope this tool helps streamline your workflow and makes your job a little easier. Whether you’re testing new features, building data pipelines, or prototyping applications, I believe the fake-database-generator can be a valuable addition to your toolkit.


I would love to hear your thoughts on it—how it works for you, how it could be improved, and any ideas you have for making it even better. Your feedback is invaluable and will help shape any future of this tool. Feel free to share your experiences, suggestions, or even challenges you encounter along the way. Let’s work together to make data engineering just a bit more efficient for everyone.


Get Involved

  • GitHub: Check out the project on GitHub. Contributions and feedback are always welcome!

  • LinkedIn: Connect with me on LinkedIn to discuss data engineering, share ideas, or collaborate on future projects.

13 views0 comments

Comments


bottom of page