question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with CompositeKey

See original GitHub issue

I 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:closed
  • Created 10 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
imakirocommented, Jun 26, 2017

I have this issue too, it’s still a problem. Is there any news?

0reactions
coleifercommented, Jul 7, 2017

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”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found