i trying create one-to-many relationship between yahootickersymbol , pricehistory database. however, have data in pricehistory database without keys relating yahootickersymbol database. there way create relationship without violating foreign key constraint?
class yahootickersymbols(models.model): yahoo_ticker_number = models.autofield(primary_key=true, ) yahoo_ticker_symbol = models.charfield(max_length=20, ) yahoo_company_name = models.charfield(max_length=120, ) class meta: ordering = ['yahoo_company_name', ] verbose_name_plural = "yahoo ticker symbols" def __str__(self): return "%s %s." % (self.yahoo_company_name, self.yahoo_ticker_symbol) class pricehistory(models.model): price_data_number = models.autofield(primary_key=true, ) yahoo_ticker_symbol = models.charfield(max_length=20, ) price_date = models.datefield(auto_now=false, auto_now_add=false, ) price_open = models.decimalfield(max_digits=7, decimal_places=3, ) price_high = models.decimalfield(max_digits=7, decimal_places=3, ) price_low = models.decimalfield(max_digits=7, decimal_places=3, ) price_close = models.decimalfield(max_digits=7, decimal_places=3, ) price_adjusted_close = models.decimalfield(max_digits=7, decimal_places=3, ) price_volume = models.bigintegerfield() yahootickersymbol = models.foreignkey(yahootickersymbols, blank=true, null=true, on_delete=models.set_null, ) class meta: ordering = ['yahoo_ticker_symbol', 'price_date', ] verbose_name_plural = "historical prices" def __str__(self): return "%s - %s : %s" % (self.yahoo_ticker_symbol, self.price_date, self.price_close)
the code in migration file follows:
class migration(migrations.migration): dependencies = [ ('investments', '0009_auto_20160124_1517'), ] operations = [ migrations.alterfield( model_name='pricehistory', name='yahootickersymbol', field=models.foreignkey(to='investments.yahootickersymbols', on_delete=django.db.models.deletion.set_null, null=true, blank=true), ), ]
i realized made mistake in process of running makemigrations
command.
previously, did not set null=true
models.foreignkey
, ran makemigrations
command. after realizing error, included null=true
within models.py
. however, did not delete previous migration files. such, django stuck in trying run old migration files though these not possible effected.
to resolve this, need run showmigrations
understand migration files have not been included understand root of problem.
Comments
Post a Comment