Skip to content

Instantly share code, notes, and snippets.

@apurvc
Created September 23, 2024 08:46
Show Gist options
  • Save apurvc/31e1f89e2b67171c2fb146a3787111c0 to your computer and use it in GitHub Desktop.
Save apurvc/31e1f89e2b67171c2fb146a3787111c0 to your computer and use it in GitHub Desktop.
from faker import Faker
import random
import csv
fake = Faker()
# Number of users and orders to generate
num_users = 100
num_orders = 20000
# Step 1: Generate Data for the users Table (Parent Table)
users = []
for user_id in range(1, num_users + 1):
user = {
'user_id': str(fake.random_int(min=1, max=99999999)).zfill(8),
'first_name': fake.first_name(),
'last_name': fake.last_name(),
'email': fake.email()
}
users.append(user)
# Save users data to a CSV file (Optional)
with open('users.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['user_id', 'first_name', 'last_name', 'email'])
for user in users:
writer.writerow([user['user_id'], user['first_name'], user['last_name'], user['email']])
# Step 2: Generate Data for the orders Table (Child Table)
orders = []
for order_id in range(1, num_orders + 1):
order = {
'order_id': order_id,
'user_id': random.choice(users)['user_id'], # Pick a random user_id from the users list
'product_name': fake.word(),
'order_date': fake.date_this_year().strftime('%Y-%m-%d')
}
orders.append(order)
# Save orders data to a CSV file (Optional)
with open('orders.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['order_id', 'user_id', 'product_name', 'order_date'])
for order in orders:
writer.writerow([order['order_id'], order['user_id'], order['product_name'], order['order_date']])
print("Data for 'users' and 'orders' tables generated successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment