Issues with CompositeKey
See original GitHub issueI have two models. Model A has a CompositeKey as primary_key, defined as indicated in [0]. Model B references model A with a ForeignKeyField(A) as indicated in [1]. Its seems that something is wrongly defined and raises an exception when tables are created in sqlite engine.
Below is the full code reproducing the problem.
Any help would be appreciated, Thanks in advance!
[0] http://peewee.readthedocs.org/en/latest/peewee/api.html#CompositeKey [1] http://peewee.readthedocs.org/en/latest/peewee/api.html#ForeignKeyField
context_db = SqliteDatabase(":memory:")
class ContextModel(Model):
class Meta:
database = context_db
class A(ContextModel):
id = IntegerField()
another_thing = CharField()
description = TextField()
class Meta:
primary_key = CompositeKey('id','description')
class B(ContextModel):
fk = ForeignKeyField(A)
A.create_table()
B.create_table()
And the second invocation raises:
/.../python2.7/site-packages/peewee-2.1.6-py2.7.egg/peewee.pyc in get_db_field(self)
709 to_pk = self.rel_model._meta.primary_key
710 if not isinstance(to_pk, PrimaryKeyField):
--> 711 return to_pk.get_db_field()
712 return super(ForeignKeyField, self).get_db_field()
713
AttributeError: 'CompositeKey' object has no attribute 'get_db_field'
Issue Analytics
- State:
- Created 10 years ago
- Comments:19 (9 by maintainers)
Top Results From Across the Web
database design - Are composite primary keys bad practice?
Composite PRIMARY KEY s are often a very "good thing" and the only way to model natural situations that occur in everyday life!...
Read more >Composite Primary Keys : Good or Bad? - Stack Overflow
There is no conclusion that composite primary keys are bad. The best practice is to have some column or columns that uniquely identify...
Read more >7 reasons not to use composite keys | by Pablo Dall'Oglio
7 reasons not to use composite keys · 1. Persistence is much simpler · 2. Object Cache (Identity Map) · 3. Registration maintenance...
Read more >Composite Primary Key / ORM - Documentation - Deepkit
This way of modelling your database has advantages and disadvantages. We believe that composite primary keys have enormous practical disadvantages that don't ...
Read more >Composite Key in SQL: Your Ultimate Guide to Mastery
Composite keys in SQL prove to be useful in those cases where you have a requirement of keys that can uniquely identify records...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have this issue too, it’s still a problem. Is there any news?
I still dislike having some kind of a ForeignKeyField that points to a CompositePrimaryKey. You can always add a foreign key constraint and a handy property in-place of some kind of “CompositeForeignKeyField”.