0
我在django中創建模型。這是我在模型文件中的配置。我想創建一個引用縣和公民的表governmentoffical
。但是,當我運行manage.py sqlal時,它在政府官方表格中更改了我的專欄名稱。django模型外鍵創建索引
class counties(models.Model):
name = models.CharField(max_length=50, primary_key=True, unique=True)
population = models.IntegerField()
governer = models.CharField(max_length=75)
class citizen(models.Model):
ssn = models.CharField(max_length=40, primary_key=True)
citizen_name = models.CharField(max_length=50, unique=True)
age = models.IntegerField()
income = models.IntegerField()
username = models.CharField(max_length=30)
password = models.CharField(max_length=30)
class governmentOfficial(models.Model):
countyName = models.ForeignKey(counties, primary_key=True)
offical_name = models.ForeignKey(citizen, to_field='citizen_name')
position = models.CharField(max_length=50)
year_elected = models.IntegerField(max_length=4)
party = models.CharField(max_length=25)
county_username=models.CharField(max_length=20)
county_password=models.CharField(max_length=20)
這是我的sql輸出。
CREATE TABLE `troywebsite_governmentofficial` (
`countyName_id` varchar(50) NOT NULL PRIMARY KEY,
`offical_name_id` varchar(50) NOT NULL,
`position` varchar(50) NOT NULL,
`year_elected` integer NOT NULL,
`party` varchar(25) NOT NULL,
`county_username` varchar(20) NOT NULL,
`county_password` varchar(20) NOT NULL
)
;
ALTER TABLE `troywebsite_governmentofficial` ADD CONSTRAINT `countyName_id_refs_name_2c9a2c94` FOREIGN KEY (`countyName_id`) REFERENCES `troywebsite_counties` (`name`);
ALTER TABLE `troywebsite_governmentofficial` ADD CONSTRAINT `offical_name_id_refs_citizen_name_6f795c2a` FOREIGN KEY (`offical_name_id`) REFERENCES `troywebsite_citizen` (`citizen_name`);
CREATE INDEX `troywebsite_governmentofficial_effdd6a5` ON `troywebsite_governmentofficial` (`offical_name_id`);
CREATE INDEX `troywebsite_solider_c5b4e228` ON `troywebsite_solider` (`solider_name_id`);
有沒有辦法拿出在governmentofficial的CREATE INDEX
,並留下來是countyName
和Official_name
爲列?
有什麼特別的原因,你想要的ID列不像ID列? –
沒有特別的原因。我只是想知道爲什麼Django正在改變列的名稱。 – user1817081
[因爲它就是這樣](https://docs.djangoproject.com/en/dev/ref/models/fields/#database-representation),因爲列名對代碼無關緊要。 –