我有3个模型-站点,组织和机构定义如下。问题是,当我运行迁移时,出现以下错误:
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 42, in defaulterrorhandler
raise errorvalue
django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'None) NOT NULL, `site_uid` varchar(50) NOT NULL)' at line 1")
site.py
class Site(models.Model):
STATUS = (
('Active', 'Active'),
('Pending', 'Pending'),
('Incomplete', 'Incomplete')
)
uid = models.CharField(primary_key=True, max_length=50, unique=True, editable=False)
site_name = models.CharField(max_length=100, null=False, default='')
country = models.CharField(max_length=100, null=False, default='')
postcode = models.CharField(max_length=20, null=False, default='')
full_address = models.TextField(max_length=200, null=False, default='')
status = models.CharField(max_length=20, choices=STATUS, default='Pending')
created_on = models.DateTimeField(default=date.today)
def __str__(self):
return self.site_name
organization.py
class Organization(models.Model):
STATUS = (
('Active', 'Active'),
('Pending', 'Pending'),
('Incomplete', 'Incomplete')
)
uid = models.CharField(primary_key=True, max_length=50, unique=True, editable=False, default=uuid.uuid4)
organization_name = models.CharField(max_length=100, null=False, default='')
country = models.CharField(max_length=100, null=False, default='')
postcode = models.CharField(max_length=20, null=False, default='')
full_address = models.TextField(max_length=50, null=True, blank=True)
status = models.CharField(max_length=20, choices=STATUS, default='Pending')
created_on = models.DateTimeField(default=date.today)
def __str__(self):
return self.organization_name
institution.py
class Institution(models.Model):
uid = models.CharField(primary_key=True, max_length=50, unique=True, editable=False, default=uuid.uuid4)
organization_uid = models.ForeignKey(Organization, on_delete=models.CASCADE,
editable=False)
site_uid = models.ForeignKey(Site, on_delete=models.CASCADE, editable=False)
def __str__(self):
return self.uid
定义模型的方式是否有问题?任何提示将不胜感激!
编辑:
这是初始迁移文件:
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Institution',
fields=[
('uid', models.CharField(default=uuid.uuid4, editable=False, max_length=50, primary_key=True, serialize=False, unique=True)),
],
),
migrations.CreateModel(
name='Organization',
fields=[
('uid', models.CharField(default=uuid.uuid4, editable=False, max_length=50, primary_key=True, serialize=False, unique=True)),
('organization_name', models.CharField(default=b'', max_length=100)),
('country', models.CharField(default=b'', max_length=100)),
('postcode', models.CharField(default=b'', max_length=20)),
('full_address', models.TextField(blank=True, max_length=50, null=True)),
('status', models.CharField(choices=[(b'Active', b'Active'), (b'Pending', b'Pending'), (b'Incomplete', b'Incomplete')], default=b'Pending', max_length=20)),
('created_on', models.DateTimeField(default=django.utils.datetime_safe.date.today)),
],
),
migrations.CreateModel(
name='Site',
fields=[
('uid', models.CharField(editable=False, max_length=50, primary_key=True, serialize=False, unique=True)),
('site_name', models.CharField(default=b'', max_length=100)),
('country', models.CharField(default=b'', max_length=100)),
('postcode', models.CharField(default=b'', max_length=20)),
('full_address', models.TextField(default=b'', max_length=200)),
('status', models.CharField(choices=[(b'Active', b'Active'), (b'Pending', b'Pending'), (b'Incomplete', b'Incomplete')], default=b'Pending', max_length=20)),
('created_on', models.DateTimeField(default=django.utils.datetime_safe.date.today)),
],
),
migrations.CreateModel(
name='Transaction',
fields=[
('uid', models.CharField(editable=False, max_length=50, primary_key=True, serialize=False, unique=True)),
('created_on', models.DateTimeField(default=django.utils.datetime_safe.date.today)),
('amount', models.DecimalField(decimal_places=2, default=b'0', max_digits=6)),
('currency', models.CharField(blank=True, default=b'\xc2\xa3')),
('site_uid', models.ForeignKey(db_column=b'site_uid', on_delete=django.db.models.deletion.CASCADE, to='main.Site')),
],
),
migrations.CreateModel(
name='User',
fields=[
('uid', models.CharField(default=uuid.uuid4, editable=False, max_length=50, primary_key=True, serialize=False, unique=True)),
('email', models.CharField(blank=True, default=b'', max_length=20)),
('password', models.CharField(blank=True, default=b'', max_length=20)),
('first_name', models.CharField(max_length=20)),
('last_name', models.CharField(max_length=20)),
('full_address', models.CharField(max_length=50)),
('country', models.CharField(max_length=20)),
('status', models.CharField(choices=[(b'Active', b'Active'), (b'Pending', b'Pending'), (b'Locked', b'Locked'), (b'Validated', b'Validated')], default=b'Pending', max_length=20)),
('role', models.CharField(choices=[(b'SuperAdmin', b'SuperAdmin'), (b'OrganizationAdmin', b'OrganizationAdmin'), (b'SiteAdmin', b'SiteAdmin'), (b'User', b'User')], default=b'User', max_length=20)),
('date_joined', models.DateField(default=django.utils.datetime_safe.date.today)),
('password_last_updated', models.DateField(default=django.utils.datetime_safe.date.today)),
('last_logged_in', models.DateField(default=django.utils.datetime_safe.date.today)),
('institution', models.ForeignKey(db_column=b'institution_uid', on_delete=django.db.models.deletion.CASCADE, to='main.Institution')),
],
),
migrations.AddField(
model_name='institution',
name='organization_uid',
field=models.ForeignKey(db_column=b'organization_uid', editable=False, on_delete=django.db.models.deletion.CASCADE, to='main.Organization'),
),
migrations.AddField(
model_name='institution',
name='site_uid',
field=models.ForeignKey(db_column=b'site_uid', editable=False, on_delete=django.db.models.deletion.CASCADE, to='main.Site'),
),
]
问题可能出在我声明FK的方式上,因为除FK之外,所有字段均出现在mysql表中。
答案 0 :(得分:0)
我的模型存在的问题是我在一个模型中定义了一个名为currency
的字段,其中我为 charField < / p>
'£'
谢谢大家的帮助!