Note that there are some explanatory texts on larger screens.

plurals
  1. POORMLite AutoIncrement doesn't work in PostgreSQL
    primarykey
    data
    text
    <p>For some models in ORMLite I use <code>Guid</code> as <code>Primary/Foreign Key</code> - it allows to do bulk inserts without the need to do <code>GetLastInsertedId()</code> after each record inserted. I also use <code>ShortId</code> of <code>int</code> type for search purposes or passing in URL parameter (like blog post integer id) that should be autoincremented after each insert.</p> <p>I don't understand how to make <code>[AutoIncrement]</code> work on non-primary key column in PostgreSQL. What I'm doing wrong?</p> <p>Here is an example of my class:</p> <pre><code>[Alias("ProductProperties")] public class ProductProperty : IHasShortId, IHasName { public ProductProperty() { Id = Guid.NewGuid(); } [PrimaryKey] public Guid Id { get; set; } [AutoIncrement] public int ShortId { get; set; } public string Name { get; set; } [ForeignKey(typeof(ProductPropertyType))] public int PropertyTypeId { get; set; } } </code></pre> <p>It generates create table script with <code>short_id</code> as <code>integer</code>, not <code>serial</code>:</p> <pre><code>CREATE TABLE product_properties ( id uuid NOT NULL, short_id integer NOT NULL, name text, property_type_id integer NOT NULL, CONSTRAINT product_properties_pkey PRIMARY KEY (id), CONSTRAINT "FK_product_properties_product_property_types_PropertyTypeId" FOREIGN KEY (property_type_id) REFERENCES product_property_types (short_id) MATCH Unknown ON UPDATE NO ACTION ON DELETE NO ACTION ) WITH ( OIDS=FALSE ); ALTER TABLE product_properties OWNER TO postgres; </code></pre> <p><strong>UPDATE:</strong></p> <p>I was able to fix this by patching ORMLite source code: <a href="https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs#L127-L128" rel="nofollow">https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs#L127-L128</a></p> <p>Commented the <code>isPrimaryKey &amp;&amp;</code> line.</p> <p>Also edited ServiceStack.OrmLite.OrmLiteDialectProviderBase.cs lines 385-403</p> <p>from</p> <pre><code>if (isPrimaryKey) { sql.Append(" PRIMARY KEY"); if (autoIncrement) { sql.Append(" ").Append(AutoIncrementDefinition); } } else { if (isNullable) { sql.Append(" NULL"); } else { sql.Append(" NOT NULL"); } } </code></pre> <p>to</p> <pre><code> if (autoIncrement) { sql.Append(" ").Append(AutoIncrementDefinition); } if (isPrimaryKey) { sql.Append(" PRIMARY KEY"); } else { if (isNullable) { sql.Append(" NULL"); } else { sql.Append(" NOT NULL"); } } </code></pre> <p>Why does ORMLite restricts AutoIncrement to primary keys only?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload