Comments (11)
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.
Interesting point, and one I didn't know about. I'll take a look into it -- thank you!
from peewee.
I've added ability to specify a unique keyword arg when creating fields:
c3460e3
from peewee.
This is awesome -- thank you very much!
from peewee.
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.
+1 , in some case it would be important, agree it should be possible to use a CharField
from peewee.
+1 to a primarykey CharField
from peewee.
Also relevant: biginteger fields
from peewee.
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.
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.
Merged, syntax is
class NonIntPK(TestModel):
id = PrimaryKeyField(column_class=VarCharColumn)
name = CharField(max_length=10)
from peewee.
Related Issues (20)
- Cannot introspect table with foreign key constraint in another schema HOT 1
- The SUM function does not work as expected HOT 1
- Cannot create BlobField with mysql-connector HOT 2
- Bulk inserts with defaults only HOT 4
- A bug with paginate() and get()/get_or_none? HOT 1
- BUG?After mysql call insert_many, execute always returns 1? HOT 9
- peewee.fn...() as a class attribute becomes an abstract method HOT 2
- After a period of inactivity, peewee fails to perform database operations through the database connection HOT 6
- ObjectIdAccessor HOWTO HOT 3
- Bulk update query with multiple fields failing silently HOT 3
- Unsure how to use SqliteQueuedDatabase across multiple processes HOT 2
- HowTo: Run Peewee Query without Model Definition HOT 4
- psycopg version 3 HOT 1
- How do I successfully construct an object graph when joining with a subquery? HOT 1
- The postgres database table I am trying to query has special characters in the column field names HOT 4
- To have a field mapped to backref HOT 3
- Inclusion of field names in ManyToMany "through" table naming HOT 1
- db_value inside hybrid property and expression HOT 1
- Allias param for peewee.ModelSelect.join HOT 7
- Int to float conversion does not work for IntegerField HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peewee.