| この記事の要点 |
|---|
|
デフォルトのテーブル名規則
# プロジェクト構造
myproject/
├── manage.py
└── apps/
└── users/ # アプリ名: "users"
├── apps.py # name = "users"
└── models.py # Profile モデル
# models.py
class Profile(models.Model):
name = models.CharField(max_length=100)
# 生成される DB テーブル名:
# → "users_profile" (アプリ名_モデル名)
命名規則の詳細
| アプリ名 | モデル名 | テーブル名 |
|---|---|---|
| users | Profile | users_profile |
| users | UserSettings | users_usersettings (小文字化、_ なし) |
| blog | BlogPost | blog_blogpost |
| shop | Order | shop_order |
| auth (Django 標準) | User | auth_user |
注意: CamelCase は小文字化されるが、単語間に _ は入らない:
class UserSettings(models.Model):
...
# → users_usersettings (× users_user_settings)
カスタムテーブル名の指定
class Profile(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = "custom_profile" # ← 任意のテーブル名
# → "custom_profile" テーブルが作成される
既存 DB との連携
# 既存の "user_profiles" テーブルを使いたい
class UserProfile(models.Model):
name = models.CharField(max_length=100, db_column="user_name") # カラム名指定
email = models.EmailField(db_column="email_address")
class Meta:
db_table = "user_profiles"
managed = False # ← Django で migrate しない(テーブル既存)
app_label の指定
# アプリ "shop" 内で他アプリ "blog" のテーブル名を使いたい
# 通常は無理だが、app_label で明示可能
# apps/shop/models.py
class Article(models.Model):
title = models.CharField(max_length=100)
class Meta:
app_label = "blog"
db_table = "blog_article" # blog_article として作成
# 注: 通常はこれを避け、proper な location にモデルを置く
Django 標準アプリのテーブル名
| アプリ | モデル | テーブル名 |
|---|---|---|
| auth | User | auth_user |
| auth | Group | auth_group |
| auth | Permission | auth_permission |
| contenttypes | ContentType | django_content_type |
| sessions | Session | django_session |
| admin | LogEntry | django_admin_log |
マイグレーション後にテーブル名を変更
# 既に "users_profile" として作成済み
# "custom_profile" に変更したい
# 1. モデルに db_table を追加
class Profile(models.Model):
...
class Meta:
db_table = "custom_profile"
# 2. makemigrations
$ python manage.py makemigrations
Migrations for 'users':
users/migrations/0002_alter_profile_options.py
- Change Meta options on profile
# 3. migrate
$ python manage.py migrate
# → RENAME TABLE users_profile TO custom_profile が実行される
現在のテーブル名を確認
# Python シェルから
$ python manage.py shell
>>> from users.models import Profile
>>> Profile._meta.db_table
'users_profile'
# SQL クエリで
>>> from django.db import connection
>>> connection.introspection.table_names()
['auth_group', 'auth_user', 'users_profile', ...]
# 個別モデルの SQL を見る
>>> from django.db import connection
>>> print(connection.queries[-1]) # 直前のクエリ
# manage.py sqlmigrate でマイグレーション SQL を表示
$ python manage.py sqlmigrate users 0001
CREATE TABLE "users_profile" (...);
命名衝突対策
異なるアプリで同名モデルがあると、テーブル名で区別:
# blog/models.py
class Post(models.Model):
title = models.CharField(max_length=100)
# → blog_post
# news/models.py
class Post(models.Model):
title = models.CharField(max_length=100)
# → news_post
# 衝突しない(アプリ名で prefix される)
# ForeignKey で参照する場合は "app.Model" 形式
class Comment(models.Model):
post = models.ForeignKey('blog.Post', on_delete=models.CASCADE)
注意点
- 名前変更には migrate 必須:
db_table変更 → makemigrations → migrate - 本番でのリネームは慎重に: 大規模テーブルは ALTER TABLE が長時間ロック
- managed=False: 既存 DB の場合、Django の migrate 対象から外す
- 命名規則の混在: snake_case と CamelCase が混ざらないように統一
- db_column 指定: カラム名も同様にカスタマイズ可能
関連記事
子ページ
子ページはありません
同階層のページ
- Model の定義方法
- マイグレーションファイルの作成
- テーブル定義の確認
- テーブルの作成
- テーブル名 = アプリケーション名 + モデル名の設定変更
- モデルの中身を確認
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- プロジェクトをTomcatプロジェクトとして認識させる方法 2026-10-07 22:32:50
- MySQLの1366 Incorrect string value|Laravelの文字コード・絵文字エラー 2026-10-07 21:54:03
- curlの証明書ホスト名不一致|旧エラー51・現行60の確認と対処 2026-10-07 21:54:03
- LaravelのMassAssignmentException|fillableの原因と安全な対処 2026-10-07 21:54:03
- Eclipse で Tomcat の起動ログがコンソールに出ない時の確認手順 2026-10-07 21:54:02
- MySQLにおける中央値(Median)の導き方(バージョン8未満) 2026-10-07 13:49:45
- getInputForward 2026-10-07 13:41:15
- JSONから配列に変換 2026-10-07 13:41:15
- ビューから値をモデルに格納しコントローラーで受け取る方法 2026-10-07 13:23:41
- Laravelのテーブル作成と定義変更|マイグレーション・up/down・注意点 2026-10-07 13:23:41
- NumPy 配列に要素を追加する方法 (append / concatenate) 2026-10-07 13:23:41
- MariaDB・MySQLで現在日時を取得する方法|NOW・タイムゾーン・保存型 2026-10-07 13:13:36
- 【django】テンプレートで定数を使用する方法 2026-10-07 13:10:15
- Spring BootにおけるApplication.propertiesの環境依存設定の分割方法 2026-10-07 12:09:35
- Not supported for DML operations【Springエラー】 2026-10-07 11:09:38