Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration primary key #1032

Closed
ostapetc opened this issue Oct 21, 2013 · 12 comments
Closed

migration primary key #1032

ostapetc opened this issue Oct 21, 2013 · 12 comments

Comments

@ostapetc
Copy link

Why migration creates primary key without unsigned attribute?

@qiangxue
Copy link
Member

What is "unsigned attribute"? What is the problem?

@klimov-paul
Copy link
Member

I suppose @ostapetc means migration should create ‘unsigned integer’ instead of simple ‘integer’ for the primary key with auto increment. Unsigned types have higher maximum value, then normal, so the auto increment less likely will be exhausted.

Similar request was already posted at Yii1:
yiisoft/yii#1923

@qiangxue
Copy link
Member

I see. Does my comment yiisoft/yii#1923 (comment) make sense to you then?

@cebe
Copy link
Member

cebe commented Oct 22, 2013

Comment makes sense, when you need an unsigned integer you can write your own column definition instead of using the abstract types.

@cebe cebe closed this as completed Oct 22, 2013
@ostapetc
Copy link
Author

@klimov-paul thanks for help

@klimov-paul
Copy link
Member

@ostapetc, I understand you are upset, but I am afraid there is nothing I can do here. But, OK, I’ll try.
Let’s have a look on this @qiangxue’s comment once more:

Using int has the benefit that it can be represented in PHP as an integer. If unsigned, for 32 bit system, we would have to use string to represent it

Representing the high integer at 32-bit system could be a problem only, if its actual value will overflow PHP max_int for 32-bit system. In general it should be safe. Also just when you last saw 32-bit server system?

Also although unsigned int doubles the size

Well, I have checked the MySQL manual once again
http://dev.mysql.com/doc/refman/5.1/en/integer-types.html
It says rather clear that both ‘signed’ and ‘unsigned’ integer has just the same size.
Do you remember difference between ‘signed’ and ‘unsigned’ data types? At the lowest level (CPU) any integer value is stored in binary representation, for example number ‘8’ will be ‘00001000’. The trick is now to remember the sign of the number: ‘+8’ or ‘-8’. To track the sign the first bit is used. So ‘+8’ will be ‘00001000’, while ‘-8’ will be ‘10001000’. Tracking the sign of the number decrease it maximum value, because the first bit, which covers 2^7 = 128 for single byte values, is reserved to remember sign of the number.
Using ‘unsigned’ integer instead of ‘signed’ one increases its maximum positive value by the cost of disallowing negative value storage. It is just what is needed for ‘Auto Increment’ primary key, otherwise you are losing 1 bit for values, because auto increment value is always more then 0.

Do not forget, that using magic ‘pk’ in migration increases its university, so migration written for MySQL can be applied to PostgreSQL as well. While custom PK definition will be stuck for the particular DBMS.

@qiangxue, @cebe, if you are still think this feature should not be implemented, just close this issue once more.

@klimov-paul klimov-paul reopened this Oct 22, 2013
@ostapetc
Copy link
Author

+1
ps: unsigned attribute provides some data integrity. I can be sure that my pk values has only positive integers

@qiangxue
Copy link
Member

PHP on Windows are usually 32bits build since php.net states the x64 builds of PHP for Windows should be considered experimental.

Also imagine in practice, if you are defining a PK for a table which is very likely to have billions of rows, do you think you would consider using unsigned int or bigint? The extra doubling doesn't really relieve you from worrying the key space.

@ostapetc
Copy link
Author

"consider using unsigned int or bigint?" - i will choose bigint unsigned, because i do not plan store negative integers

@cebe cebe reopened this Oct 22, 2013
@qiangxue
Copy link
Member

qiangxue commented Nov 1, 2013

Many DBMS actually don't even support unsigned types: mssql, oracle, pgsql, cubrid...

@cebe
Copy link
Member

cebe commented Dec 17, 2013

I do not remember why I reopened it but as @qiangxue said there are many dbms that do not support unsigned integer. if you want your PK to be unsigned you can write explicit column definition. we will not add it to the abstract types for consistency.

@cebe cebe closed this as completed Dec 17, 2013
@cebe
Copy link
Member

cebe commented Dec 17, 2013

Do not forget, that using magic ‘pk’ in migration increases its university, so migration written for MySQL can be applied to PostgreSQL as well. While custom PK definition will be stuck for the particular DBMS.

btw: If you want to create portable code, unsigned is not your friend. So not an argument for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants