How to use a custom postgresql type in seaORM? - postgresql

I have the following table in postgres
create table retailers(id: integer, name: citext);
citext is just case insensitive text in postgres. How do I modify the Entity creation in SeaORM to support that?
When I generate entities in SeaORM in rust, I get the following error:
error: Missing macro attribute, either `string_value` or `num_value` should be specified or specify repr[X] and have a value for every entry
--> src/entity/sea_orm_active_enums.rs:10:5
How do I "register" new types in seaORM in Rust?

Related

What column type should UUID be in postgreSQL?

I would like to use UUIDs has my primary key, and I am generating them using the built-in gen_random_uuid() expression for DEFAULT.
However, I don't know what column type to use for UUIDs. When I use uuid or UUID, I get the following error:
PostgreSQL said: column "id" cannot be cast automatically to type uuid
Hint: You might need to specify "USING id::uuid".
Is there a native UUID column type? Should I just be using varchar(255)?
Thanks.
PostgreSQL supports a UUID data type 'out of the box' indeed: https://www.postgresql.org/docs/current/datatype-uuid.html

Size of a GIN index in postgreSQL

I have created a model in Django.
class MyModel(models.Model):
features = TextField(blank=True, default='')
There are several possible ways to store the data in the feature field. Some examples below.
feature1;feature2
feature1, feature2
feature1,feature2
And so on. I created a GIN index for that field using migrations.RunSQL() (thanks to the following answer). The postgreSQL command looks as follows
CREATE INDEX features_search_idx ON "mymodel" USING gin (regexp_split_to_array("mymodel"."features", '[,;\\s]+'));
Now I need to check the size of the created index in my database. I tried to do it with the following commands
SELECT pg_size_pretty (pg_indexes_size("mymodel"."features_search_idx"));
SELECT pg_size_pretty(pg_indexes_size("features_search_idx")) FROM "mymodel";
The latter one failed with ERROR: column "features_search_idx" does not exist and the former one failed with ERROR: missing FROM-clause entry for table "mymodel".
How can I check the index size?
pg_indexes_size takes an argument of type regclass, that is an object ID that is represented as a string that is the object name. So if you don't supply an object ID, you have to supply a string (single quotes) that is the name of the table:
SELECT pg_size_pretty (pg_indexes_size('mymodel.features_search_idx'));

Using postgresql extensions with Spring Boot multi tenant application

I am working on a multi tenant Spring Boot application using Postgresql and different schemas for tenants. Everything works fine until we need to use Postgresql's extensions, then we got errors about missing types.
ERROR: function text2ltree(character varying) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 266
The extension is installed using Liquibase and I have updated the changesets to perform the following:
CREATE SCHEMA myschema;
CREATE EXTENSION IF NOT EXISTS ltree WITH SCHEMA myschema;
ALTER DATABASE mydb SET search_path TO "$user", myschema
Then I can control where the extension is installed and updating the search path should avoid to specify the schema all the time. I have run the Liquibase migration and check Postgresql configuration for search_path and it works. I can run queries (using Squirrel) without the need to include the schema prefix for the ltree extension types and functions.
It does not work as expected as I still got the same error message. If I explicitly prefix the ltree types and functions with myschema, I got an error indicating that the operator is not found.
ERROR: operator does not exist: myschema.ltree <# myschema.ltree
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 263
For multi tenancy, we implement a custom org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider that sets the schema in the MultitenantConnectionProvider.getAnyConnection() method.
I want to simplify the setup so I want to avoid to include always the extension's schema, besides the search_path configuration, I can not found anything related. Is there a way to implement the described setup to prevent prefixing with the extension schema in all the code?
If I install the extensions in the pg_catalog schema, then everything works as expected. See this answer.
I want to prevent to install the extensions in the pg_catalog schema so, a closer look to the PGConnection class reveals that the method to set the default schemas updates the search_path value to the schema given.
StringBuilder sb = new StringBuilder();
sb.append("SET SESSION search_path TO '");
Utils.escapeLiteral(sb, schema, getStandardConformingStrings());
sb.append("'");
stmt.executeUpdate(sb.toString());
It is escaping the schema argument, therefore if I try to use multiple schemas separated by commas, the whole expression is escaped.
The solution has been to create my own setSchema method in the MultiTenantConnectionProvider class. It does the same as the code above but adding my extensions' schema to the search_path value.

How change a column type in Firebird 3

Since Firebird 3, I can't modify a column type.
Before I use this kind of update:
update RDB$RELATION_FIELDS set
RDB$FIELD_SOURCE = 'MYTEXT'
where (RDB$FIELD_NAME = 'JXML') and
(RDB$RELATION_NAME = 'XMLTABLE')
because I get ISC error 335545030 ("UPDATE operation is not allowed for system table RDB$RELATION_FIELDS").
Maybe there is another way in Firebird 3?
Firebird 3 no longer allows direct updates to the system tables, as that was a way to potentially corrupt a database. See also System Tables are Now Read-only in the release notes. You will need to use DDL statements to do the modification.
It looks like you want to change the data type of a column to a domain. You will need to use alter table ... alter column ... for that. Specifically you will need to do:
alter table XMLTABLE
alter column JXML type MYTEXT;
This does come with some restrictions:
Changing the Data Type of a Column: the TYPE Keyword
The keyword TYPE changes the data type of an existing column to
another, allowable type. A type change that might result in data loss
will be disallowed. As an example, the number of characters in the new
type for a CHAR or VARCHAR column cannot be smaller than the existing
specification for it.
If the column was declared as an array, no change to its type or its
number of dimensions is permitted.
The data type of a column that is involved in a foreign key, primary
key or unique constraint cannot be changed at all.
This statement has been available since before Firebird 1 (InterBase 6.0).
Firebird 2.5 manual, chapter Data Definition (DDL) Statement, section TABLE:
ALTER TABLE tabname ALTER COLUMN colname TYPE typename

Using PostgreSQL CITEXT Extension with jOOQ

The Postgres CITEXT extension helps with cases-insensitive data. This, for example, can be useful when working with emails. See here and here. I've defined the following table:
CREATE EXTENSION citext;
CREATE TABLE user (
user_id INTEGER PRIMARY KEY,
email CITEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
salt TEXT NOT NULL
);
and added the following to the <database> section in the pom.xml:
<forcedType>
<name>CLOB</name>
<expression>public.user.email</expression>
<types>CITEXT</types>
</forcedType>
</forcedTypes>
When I run the code generator, the fields do get generated, but there are a lot of "missing name" warnings in the log output. For example:
[INFO] Generating routine : CitextLt.java
[WARNING] Missing name : Object citext_ne holds a column without a name at position 1
Am I on the right track on integrating the CITEXT extesion with jOOQ?
If so, how do I provide these missing names?
There are two issues in this question:
Logging
The WARN level is perhaps a bit excessive. I've registered an issue to revert that to INFO: https://github.com/jOOQ/jOOQ/issues/5385
You don't have to worry about those warnings. PostgreSQL supports declaring stored procedures whose parameters are unnamed and can be referenced only by parameter index / position. jOOQ's code generator only indicates that this is "unusual" and that a synthetic parameter name is generated.
This should not affect your using CITEXT with jOOQ.
Your forced type configuration
There is currently a bug that prevents you from matching user-defined types with <types/>: http://github.com/jOOQ/jOOQ/issues/5363
Just remove your <types/> element, and it will work.