django 預設在建立 db schema 的時候
會把 table name 設成 {app_label}_{model_name},
然後 foreign key 的那個 column name 會幫你 append 一個 "_id",
另外如果你沒有指定 primary key,它也會幫你加一筆 "id"
比如說下面這個 model
class SiteInfo(models.Model): # data fileds Name = models.CharField(max_length=255) CategoryId = models.ForeignKey('SiteCategory', db_index=True)
會變成
CREATE TABLE `idsafe_testname` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `Name` varchar(255) NOT NULL, `CategoryId_id` integer NOT NULL ); ALTER TABLE `idsafe_testname` ADD CONSTRAINT `CategoryId_id_refs_Id_14001db1` FOREIGN KEY (`CategoryId_id`) REFERENCES `sitecategory` (`Id`);
如果不喜歡這樣的話,可以自己指定:
class SiteInfo(models.Model): # data fileds Id = models.AutoField(primary_key=True) Name = models.CharField(max_length=255) CategoryId = models.ForeignKey('SiteCategory', db_column='CategoryId', db_index=True) class Meta: db_table = 'siteinfo'
沒有留言:
張貼留言