-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Inserting NOT NULL auto-generated properties #25
Comments
Thanks — interesting. I hadn't run into this with primary keys. I tend to declare them the old Postgres way as $ create temporary table tmp_serial (id SERIAL PRIMARY KEY);
CREATE TABLE
$ select column_default from information_schema.columns where table_name = 'tmp_serial';
column_default
----------------------------------------
nextval('tmp_serial_id_seq'::regclass)
(1 row) However, when we do it your way (which I can see is standards-conformant and has some benefits), $ create temporary table tmp_gbdai (id INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY);
CREATE TABLE
$ select column_default from information_schema.columns where table_name = 'tmp_gbdai';
column_default
----------------
(1 row) I also hadn't run into this with triggers, just because I haven't used triggers with Zapatos, but funnily enough we've been discussing setting up triggers for our My thoughts are that we can either try to be clever, and detect these I'll look into this and make one of those happen. |
OK, based on https://www.postgresql.org/docs/current/infoschema-columns.html, for the primary key issue it looks like we should be checking As far as I can see there's no sensible way we can tell if a particular column is set by a To help me prioritise this — is it holding you up? |
Thanks for the quick reply! :) I wouldn't say that these are blockers, but the Identity fix would be super nice to have. Triggers are a bit less important, since they can be worked around by setting a default value. :) |
Right. I'll do this one as soon as I can.
Indeed — in fact, created-at and updated-at columns would I guess generally take a default of |
Yep. My case was with created_by/updated_by, which is a username string, but it too can be set to something like "Unknown" or "Anonymous" :) |
OK, 0.1.51 should treat identity columns the same as columns with a default value, which essentially they are. Do let me know if this doesn't fix things for you. And, in fact, it would also be nice to get confirmation if it does! I think I'll leave the new config options I proposed for now, and see whether the default value work-around is good enough for people. |
Thanks for the quick fix! As far as I can see, it works just as expected. :) |
I'm currently checking the possibility of using Zapatos in my project. I'm also facing this same issue. I have a number of tables with some mandatory columns whose values are being set by a before insert trigger. These columns are not identity columns, so the identity fix does not work in my case. Setting a database default does not make much sense either considering the purpose of these columns. It would be great if the additional config option (eg: optionalInsert) is added so that I can use it to make these columns optional during insert. |
OK, I'll have a think about this. |
OK, current thinking is this: we add a export interface RequiredConfig {
db: pg.ClientConfig;
}
export interface OptionalConfig {
outDir: string;
schemas: SchemaRules;
progressListener: boolean | ((s: string) => void);
warningListener: boolean | ((s: string) => void);
customTypesTransform: 'PgMy_type' | 'my_type' | 'PgMyType' | ((s: string) => string);
columnOptions: {
[k: string]: { // table name
[k: string]: { // column name
insert?: 'auto' | 'disabled' | 'optional';
update?: 'auto' | 'disabled';
};
};
};
} So you'll be able to manually disallow inserts and/or updates (e.g. if your column is set by a trigger), which removes the column from the relevant I've also now made it so This is in |
OK — I've renamed |
Hi,
I'm trying out zapatos and stumbled on one usage obstacle.
Let's say I have a table like this:
id is auto-generated and created_by is populated automatically by a trigger function BEFORE an insert.
Generated Insertable interface will then look like this:
Which means that on inserts I cannot use an object like
{title: 'some title'}
, it will complain that id and created_by are missing.Is there any way to work around this? (except for removing NOT NULL from both id and created_by)
Edit: Actually, even if I define id as
INT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
, in resulting schema it will still be marked asNOT NULL
and Insertable will still have it as required...Thanks! :)
The text was updated successfully, but these errors were encountered: