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

[BUG] Problem with RawValue('default') on composite primary key #1534

Closed
luckasfrigo opened this issue Nov 7, 2013 · 7 comments
Closed

[BUG] Problem with RawValue('default') on composite primary key #1534

luckasfrigo opened this issue Nov 7, 2013 · 7 comments

Comments

@luckasfrigo
Copy link

Hello, i have a table on MySQL where i have a composite Primary Key. The first column of te PK is a auto_increment BIGINT and the second is a VARCHAR, that has a default value.

The problem is, when i try to define the corresponding model property as RawValue('default') the value "default" is written literally in that column. It works fine on every other column.

I tried to do the same thing directly on MySQL using phpMyAdmin and it works without any problem.

Sorry if this is not a bug, but i couldn't find any explanation for that behavior.

@dreamsxin
Copy link
Contributor

Didn't understand your meaning, Can you write an example?

$robot = new Robots();
$robot->id = RawValue('default');

@luckasfrigo
Copy link
Author

Well, I have the following table, where 'id' and 'language' are both part of the PRIMARY KEY:

CREATE TABLE IF NOT EXISTS `product` (
  `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `language` varchar(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'bb',
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `slug` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `brand` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `sort` tinyint(3) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`,`language`),
  UNIQUE KEY `slug` (`slug`)
)

If I do something like...

$product = new Product();
$product->language = RawValue('default');
$product->name= 'foo';
$product->slug = 'bar';
$product->brand = RawValue('default');
$product->sort= RawValue('default');
$product->save();

... the value of 'language' is literally set as "default", not as the actual default value (which is "bb"). Notice all other default values are set correct, no matter whether they are NULL, INT or VARCHAR.

@ghost
Copy link

ghost commented Nov 18, 2013

Confirmed. This is the query Phalcon executes:

INSERT INTO `issue_1534` (`language`, `name`, `slug`, `brand`, `sort`) VALUES (?, ?, ?, default, default)

language is bound with default which is then treated as a string literal and therefore is turned into 'default'.

@ghost
Copy link

ghost commented Nov 18, 2013

This is what is passed to db::insert():

array(5) {
 [0]=>
 string(7) "default"
 [1]=>
 string(3) "foo"
 [2]=>
 string(3) "bar"
 [3]=>
 object(Phalcon\Db\RawValue)#50 (1) {
   ["_value":protected]=>
   string(7) "default"
 }
 [4]=>
 object(Phalcon\Db\RawValue)#49 (1) {
   ["_value":protected]=>
   string(7) "default"
 }
}

@ghost
Copy link

ghost commented Nov 18, 2013

I am afraid this is not going to work: when you call save(), Phalcon invokes _exists() to check whether the record exists (to find out whether it should use INSERT or UPDATE).

And this query generates an error in MySQL:

SELECT COUNT(*) "rowcount" FROM `issue_1534` WHERE `id` = 1 AND `language` = DEFAULT;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

@ghost
Copy link

ghost commented Nov 18, 2013

Workaround is to use DEFAULT(language).

@ghost ghost mentioned this issue Nov 18, 2013
phalcon pushed a commit that referenced this issue Nov 18, 2013
@ghost
Copy link

ghost commented Dec 11, 2013

Can this one be closed now?

@phalcon phalcon closed this as completed Dec 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants