我有一個非常小的遷移,看起來像這樣爲我的Django應用程序(MySQL的後端)Django的rawSQL遷移:在您的SQL語法錯誤,但SQL是正確的
operations = [
migrations.CreateModel(
name='Mileage',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('miles', models.DecimalField(max_digits=8, decimal_places=1)),
('start_location', models.CharField(max_length=255)),
('end_location', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Trip',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('start_location', models.CharField(max_length=255)),
('end_location', models.CharField(max_length=255)),
('trip_date', models.DateTimeField(verbose_name='trip date')),
],
),
migrations.RunSQL('mymileages.sql'),
]
的mymileages.sql
文件看起來是這樣的:
INSERT INTO `mileage_mileage` (`miles`, `start_location`, `end_location`) VALUES
(3.7, 'Location 1', 'Location A'),
(2.4, 'Location 2', 'Location B'),
(4.3, 'Location 3', 'Location C');
我可以在遷移之外運行該SQL,它工作正常,沒有問題。
然而,當我運行遷移,它給我的錯誤:
Programming error: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'mymileages.sql' at line 1")
我對django不太熟悉,但我認爲RunSQL使用'mymileages.sql'作爲查詢,而不是mymileages.sql文件中的正確查詢。 –
bro遷移是爲了創建表,您如何通過遷移文件插入它? – Exprator