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

Insert ignores default values #1

Open
NiklasAurora opened this issue Oct 6, 2017 · 3 comments
Open

Insert ignores default values #1

NiklasAurora opened this issue Oct 6, 2017 · 3 comments
Assignees
Labels

Comments

@NiklasAurora
Copy link
Contributor

NiklasAurora commented Oct 6, 2017

Issue: If I insert a new user into my database, DEFAULT values set in MySQL is ignored and value is set to NULL.

Environment:
PHP 7.1.8
mysql Ver 14.14 Distrib 5.7.19, for osx10.13 (x86_64) using EditLine wrapper
anax/database v1.1.4

Following test example was used in SQL:
`
DROP TABLE IF EXISTS User;

CREATE TABLE User (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
username VARCHAR(80) UNIQUE NOT NULL,
mail VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
admin INTEGER DEFAULT 0,
created DATETIME,
updated DATETIME,
deleted DATETIME,
active DATETIME
) ENGINE INNODB CHARACTER SET utf8 COLLATE utf8_swedish_ci;
`

@mosbth
Copy link
Member

mosbth commented Oct 6, 2017

To solve this the ActiveRecord must be updated to only save those properties that has changed. Currently all properties are saved, independent on its been updated or not.

@NiklasAurora
Copy link
Contributor Author

The following examples are also ignored when updating/creating new objects:

created TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,

@mosbth mosbth self-assigned this Oct 12, 2017
@mosbth mosbth added the feature label Oct 12, 2017
@NiklasAurora
Copy link
Contributor Author

After extensive testing, we noticed that if the value is not defined within the class declaration then the default value from the SQL is used. So with the above testing (created TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP) and the following class ->

/**
 * Columns in the table.
 *
 * @var integer $idComment primary key auto incremented.
 * @var integer $idQuestion not null.
 * @var string $mail not null.
 * @var string $title not null.
 * @var string $post not null.
 */

public $idComment;
public $idQuestion;
public $mail;
public $post;`

the class will use the DEFAULT value. This might be a good way to do it but documentation should be done on how to handle DEFAULT values.

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

2 participants