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 DEFAULT VALUES? #872

Closed
KeenS opened this Issue Apr 26, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@KeenS
Contributor

KeenS commented Apr 26, 2017

I have this table

CREATE TABLE users
(
	id serial NOT NULL,
	created_at timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
	PRIMARY KEY (id)
) WITHOUT OIDS;

and want to create an user using default values.

I tried to define this structure:

#[derive(Insertable)]
#[table_name="users"]
pub struct NewUser {}

but it raises this error:

error: no rules expected the token `]`
  --> model/src/lib.rs:23:19
   |
23 | #[derive(Insertable)]
   |                   ^

It is reasonable because in SQL, inserting empty values is not allowed

db=# insert into users () values (); 
ERROR:  syntax error at or near ")"  
LINE 1: insert into users () values (); 

However, with, DEFAULT VALUE you can insert values with default values.

actcast=# INSERT INTO users DEFAULT VALUES;
INSERT 0 1
actcast=# SELECT * FROM USERS;
 id |         created_at
----+----------------------------
  1 | 2017-04-26 01:12:56.90735
(1 row)

How can I use this feature through diesel? Or if not supported, could you support this (feature request)?

@KeenS

This comment has been minimized.

Contributor

KeenS commented Apr 26, 2017

Of cause, I know I can do it with sql function

sql::<Integer>("INSERT INTO users DEFAULT VALUES RETURNING id")
@sgrif

This comment has been minimized.

Member

sgrif commented Apr 30, 2017

This is a reasonable feature, but I'm unsure what I'd like the public API for it to be. If nothing else, #[derive(Insertable)] with no columns should fail to compile, as we're breaking our guarantees about runtime errors.

@KeenS

This comment has been minimized.

Contributor

KeenS commented May 1, 2017

I suppose allowing empty structs to derive Insertable (in that case, empty structs will use DEFAULT VALUES) or providing another function like insert_defaults() will do.

Eijebong added a commit to Eijebong/diesel that referenced this issue May 15, 2017

Add an insert_default_values function
This will produce the following SQL `INSERT INTO table DEFAULT VALUES`.

Fixes diesel-rs#872

Eijebong added a commit to Eijebong/diesel that referenced this issue May 15, 2017

Add an insert_default_values function
This will produce the following SQL `INSERT INTO table DEFAULT VALUES`.

Fixes diesel-rs#872

Eijebong added a commit to Eijebong/diesel that referenced this issue May 22, 2017

Add an insert_default_values function
This will produce the following SQL `INSERT INTO table DEFAULT VALUES`.

Fixes diesel-rs#872

Eijebong added a commit to Eijebong/diesel that referenced this issue May 22, 2017

Add an insert_default_values function
This will produce the following SQL `INSERT INTO table DEFAULT VALUES`.

Fixes diesel-rs#872

Eijebong added a commit to Eijebong/diesel that referenced this issue Jun 7, 2017

Add an insert_default_values function
This will produce the following SQL `INSERT INTO table DEFAULT VALUES`.

Fixes diesel-rs#872

Eijebong added a commit to Eijebong/diesel that referenced this issue Jun 11, 2017

Add an insert_default_values function
This will produce the following SQL `INSERT INTO table DEFAULT VALUES`.

Fixes diesel-rs#872
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment