Giter Club home page Giter Club logo

Comments (11)

coleifer avatar coleifer commented on May 16, 2024

Well, it really just looks like it's searching for a pk that evaluates to a nonzero...it will be tricky since the postgresql adapter checks the value of a sequence for the last insert id. Otherwise its just that bit in the BaseModel that checks for a PrimaryKeyField.

On the other hand, a non-integer primary key can be more expensive to query on and join on... might be worthwhile to implement instead the ability to add unique indexes and have something along the lines of the workaround suggested here:
http://stackoverflow.com/questions/3936182/using-a-uuid-as-a-primary-key-in-django-models-generic-relations-impact/3944247#3944247

from peewee.

matthewturk avatar matthewturk commented on May 16, 2024

Interesting point, and one I didn't know about. I'll take a look into it -- thank you!

from peewee.

coleifer avatar coleifer commented on May 16, 2024

I've added ability to specify a unique keyword arg when creating fields:
c3460e3

from peewee.

matthewturk avatar matthewturk commented on May 16, 2024

This is awesome -- thank you very much!

from peewee.

coleifer avatar coleifer commented on May 16, 2024

Glad that commit helps -- I'm actually gonna leave this one open for now as I agree it should be possible to use a TextField or something else for a primary key.

from peewee.

coderbuzz avatar coderbuzz commented on May 16, 2024

+1 , in some case it would be important, agree it should be possible to use a CharField

from peewee.

Alquimista avatar Alquimista commented on May 16, 2024

+1 to a primarykey CharField

from peewee.

coleifer avatar coleifer commented on May 16, 2024

Also relevant: biginteger fields

from peewee.

coleifer avatar coleifer commented on May 16, 2024

Started working on this in a branch: feature/non-integer-pk -- there are some interesting ramifications:

  • primary keys may not be autoincrementing which affects default save() and create() behavior
  • primary keys won't necessarily have a sequence so postgresql will need to have different last_insert_id behavior if so
  • foreign keys need to match the column type of the primary key

from peewee.

coleifer avatar coleifer commented on May 16, 2024

Alright! I think I have gotten this working. It can be seen in the field refactor branch:

https://github.com/coleifer/peewee/tree/feature/field-refactor

For a diff:

https://github.com/coleifer/peewee/compare/feature/field-refactor

Tests are passing in sqlite and postgresql. Saving models is a little bit different when using a non-integer autoincrement pk, basically you have to use the .create() or .save(force_insert=True) to save new instances otherwise the existance of a pk value will make the orm think you have saved it before and want to update it. That is the main gotcha.

Would love to get some feedback before merging, however

from peewee.

coleifer avatar coleifer commented on May 16, 2024

Merged, syntax is

class NonIntPK(TestModel):
    id = PrimaryKeyField(column_class=VarCharColumn)
    name = CharField(max_length=10)

from peewee.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.