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

Database defined NOT NULL + DEFAULT value #157

Closed
danielwestendorf opened this issue Mar 11, 2018 · 1 comment
Closed

Database defined NOT NULL + DEFAULT value #157

danielwestendorf opened this issue Mar 11, 2018 · 1 comment

Comments

@danielwestendorf
Copy link
Contributor

danielwestendorf commented Mar 11, 2018

I'm trying to create a column which is a database generated UUID default value, however, I can't seem to get this working.

What I expect to happen is that when I create a record without specifying a value for the column in question. After the record is saved the the column in question will have a UUID value.

-- +micrate Up
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE accounts (
  id BIGSERIAL PRIMARY KEY,
  name VARCHAR,
  api_key uuid NOT NULL DEFAULT uuid_generate_v4(),
  created_at TIMESTAMP,
  updated_at TIMESTAMP
);


-- +micrate Down
DROP TABLE IF EXISTS accounts;
class Account < Granite::ORM::Base
  adapter pg
  table_name accounts

  field name : String
  field api_key : String
  timestamps
end
account = Account.new(name: 'Bob')
account.save # => INSERT INTO accounts (name, api_key, created_at, updated_at) VALUES ($1, $2, $3, $4): ["Bob", nil, "2018-03-09 19:50:44", "2018-03-09 19:50:44"]

This results in null value in column "api_key" violates not-null constraint.

How do I get Granite to support default db values?

@drujensen
Copy link
Member

#166 Supporting Natural Keys allows you to disable auto incrementing. This will allow you to set your own key.

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

3 participants