-
Notifications
You must be signed in to change notification settings - Fork 376
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
PostgreSQL driver: feed DEFAULT to columns instead of NULL. #391
Comments
Given the docs at http://www.postgresql.org/docs/9.3/static/ddl-default.html, it should be safe to send 'default' instead of null/undefined except for one important case. That case is when there is a default defined and a null should be inserted. It seems to me that you would have to know whether a default was defined in the database before safely making the judgement to replace null with default. For instance, lets say you have a table as follows: CREATE TABLE table1(
id SERIAL NOT NULL,
stuff TEXT DEFAULT 'Items',
CONSTRAINT table1_pk PRIMARY KEY (id)
); The operations: INSERT INTO table1 VALUES (DEFAULT, DEFAULT); and INSERT INTO table1 VALUES (DEFAULT, NULL); Are very different, and could be meaningful to that database. Therefore the only way to safely flag where Now for having The consequence is that most of the time default and null are the same (as most fields in a database do not have defaults defined). However, it is that rare case where a default IS defined that undefined and null will behave differently. I will go ahead and see if I can make the change. The other issue, and I need some guidance here as I am new to the OSS scene, is that the change is not in ORM2, but sql-query, a package ORM depends on. It is here, in the PostgreSQL dialects folder that the change would have to be implemented. |
So a pull-request has been generated that contains the appropriate changes to allow a CREATE TABLE table1(
id SERIAL NOT NULL,
stuff TEXT,
CONSTRAINT table1_pk PRIMARY KEY (id)
); Your model definition should look like this: var table1 = db.define('table1', {
id : {type: "number", defaultValue: undefined},
stuff : {type: "text"}
},{
id: 'id',
table: 'table1'
}); And your create should look like this: table1.create([
{
stuff: "Text String goes here"
},
{
id: undefined,
stuff: "This will also work"
}
], function(err, items){}); This will allow the series that is created for the id column in PostgreSQL to be used. It will be used if the value is omitted or if the value is undefined. If you don't have a server side default, that's alright too, as PostgreSQL assigns a default value of null. If you want null in id (in this case PostgreSQL would throw an error), you have to explicitly assign null. The only changes to node-orm2 should be a test that tests for this. If the pull-request is accepted on node-sql-query, I will work on this. |
@lselvy, what about sending |
Its possible to do so. If you're asking, can we use Does that address your question? |
@lselvy, sorry for posting before thinking :) yes, it addresses precisely |
This is my first foray into working on an Open Source project, so it could Thanks! On Thu, Feb 20, 2014 at 8:01 PM, Mark notifications@github.com wrote:
"Trust in the Lord with all thine heart and lean not on thine own |
Since the PR has been merged, I'm gonna close this. |
This doesn't appear to be working in 2.1.4 -- i don't in fact see that the orm package is even using node-sql-query. |
#485 is required to allow undefined to pass through to the generator. |
@dxg This is still an issue (as far as I can see). If we have defined a DEFAULT value in the database table but not in the model, we can't insert stuff and use those DEFAULT values. An example: Let's say we have a Postgres table with two columns:
the default value for the column The query But using I would suggest (if it doesn't break other things...) that if a table column is not included in a model.create, it should be added to the INSERT query only if it has a defaultValue specified in the model. Otherwise it shouldn't be included in the INSERT-query at all. |
Using the following model
Against the following schema:
I try the following:
All give the following error:
This happens because PostgreSQL, when said to insert
NULL
, inserts as requested. To insert the default value, one should putDEFAULT
there, or omit the column when inserting.In general, a
NOT NULL
column will never allowNULL
, and forNULL
columns -- this value is the default default value, so to say.I'm suggesting to always use
DEFAULT
when insertingnull
s into PostgreSQL, or at least handleundefined
this way.The text was updated successfully, but these errors were encountered: