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

Working with default values #155

Closed
7even opened this issue Feb 24, 2015 · 6 comments
Closed

Working with default values #155

7even opened this issue Feb 24, 2015 · 6 comments
Assignees
Labels

Comments

@7even
Copy link
Contributor

7even commented Feb 24, 2015

Currently lotus-model doesn't respect default values in the database:

CREATE TABLE deployments (
    id integer NOT NULL,
    project_id integer NOT NULL,
    environment character varying NOT NULL,
    branch character varying NOT NULL,
    deployed_at timestamp with time zone DEFAULT now() NOT NULL
);
class Deployment
  include Lotus::Entity
  attributes :id, :project_id, :environment, :branch, :deployed_at

  attr_accessor :project
end

deployment = Deployment.new(project_id: 1, environment: 'production', branch: 'master')
DeploymentRepository.persist(deployment)
# Sequel::NotNullConstraintViolation: PG::NotNullViolation: ERROR:  null value in column "deployed_at" violates not-null constraint
# DETAIL:  Failing row contains (4, 1, production, master, null).

Seems like the empty deployed_at value is being written to the database. I see 2 possible ways to solve this issue: to read the default value into the entity upon it's creation (but I believe it breaks incapsulation and is not very helpful with a default value of NOW()), or to save only attributes that were changed.

@jodosha
Copy link
Member

jodosha commented Feb 24, 2015

@7even Thanks for reporting this. Which version are you using?

@7even
Copy link
Contributor Author

7even commented Feb 24, 2015

The latest one, lotus-model (0.2.4).

@jodosha
Copy link
Member

jodosha commented Feb 24, 2015

@7even Can you please also paste here the mapping for that entity?

@7even
Copy link
Contributor Author

7even commented Feb 24, 2015

Yep, here it is:

collection :deployments do
  entity     Deployment
  repository DeploymentRepository

  attribute :id,          Integer
  attribute :project_id,  Integer
  attribute :environment, String
  attribute :branch,      String
  attribute :deployed_at, Time
end

@luishurtado
Copy link

Although there is something to fix regarding the NOT NULL issue, indicating that a given attribute is required and it has a default value should also be clearly defined in the application level and not rely in the database.

@runlevel5
Copy link
Member

@7even this has been addressed in #161

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

Successfully merging a pull request may close this issue.

4 participants