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

ERROR with ActiveRecord::loadDefaultValues(), id serial and posgresql #3564

Closed
makroxyz opened this issue May 23, 2014 · 10 comments
Closed

ERROR with ActiveRecord::loadDefaultValues(), id serial and posgresql #3564

makroxyz opened this issue May 23, 2014 · 10 comments
Labels
Milestone

Comments

@makroxyz
Copy link
Contributor

using $model->loadDefaultValues() where model ID is serial, throws this error:
ERROR: invalid input syntax for integer: "nextval('tbl_mytable_id_seq'"

@cebe
Copy link
Member

cebe commented May 23, 2014

can you please post the complete CREATE TABLE statement?

@cebe cebe added this to the 2.0 RC milestone May 23, 2014
@makroxyz
Copy link
Contributor Author

sure

CREATE TABLE tbl_mytable
(
id serial NOT NULL,
user_id integer NOT NULL,
notes character varying(255),
CONSTRAINT tbl_mytable_pkey PRIMARY KEY (id)
);

@qiangxue
Copy link
Member

In general, PKs should not have default values.

@makroxyz
Copy link
Contributor Author

I didn't set any default value. My create statement is in above comment.
if I unload database to a script I can see that line:
ALTER TABLE ONLY tbl_mytable ALTER COLUMN id SET DEFAULT nextval('tbl_mytable_id_seq'::regclass);

@cebe
Copy link
Member

cebe commented May 23, 2014

looks like serial is implemented via default value internally in postgres.

@qiangxue
Copy link
Member

@makroxyz I am not saying you did wrong. ;) I mean in our core code when loading table schema, we should not load default values for PK columns. This needs to be fixed (not your code.) Sorry for confusion.

@makroxyz
Copy link
Contributor Author

@qiangxue I didn't understand I did wrong! :) I just want to give more infos ;)
Sorry for confusion too!

Another thing:
yii\db\pgsql\Schema::loadColumnSchema sets phpType to "string" for smallint columns.
$typeMap doesn't have 'int2' column type... I think smallint are int2 in PostgreSQL 9.3

should I open a new issue?

@cebe
Copy link
Member

cebe commented May 23, 2014

should I open a new issue?

yes please.

@makroxyz
Copy link
Contributor Author

I'm still having this error after $model->loadDefaultValues()
ERROR: invalid input syntax for integer: "nextval('tbl_mytable_id_seq'::regclass)"

$column->defaultValue is loaded in loadColumnSchema function. The fix doesn't unset defaultValue if column is PK...

@cebe cebe reopened this May 26, 2014
@qiangxue
Copy link
Member

Thanks! Should be fixed now.

tvdavid pushed a commit to tvdavid/yii2 that referenced this issue Jul 24, 2014
tvdavid pushed a commit to tvdavid/yii2 that referenced this issue Jul 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants