Skip to content

Issues with .HasDefaultValueSql() and custom PG functions #189

Closed

Description

@tontm1981

I am new in C#, migrating slowly from PHP, but this is not the issue. For some reasons that is not relevant here, we created some functions (one per created schema) to create a "random" string to fill PK columns (created as character varying and it's size limited to 30). Here is an example of these functions:

CREATE FUNCTION random_uid(tbl character varying) RETURNS text
LANGUAGE plpgsql
AS $$declare
    query varchar;
    results text := '';
    counter integer := 1;
begin
    WHILE counter > 0 LOOP
    	select substring(translate( atividades.gen_random_uuid()::text, '1234567890-','fghijklmno') FROM 1 FOR 30) INTO results;
        
        query := 'SELECT COUNT(1) FROM configs.' || quote_ident(tbl) || ' WHERE uid=' || quote_literal(results);
        EXECUTE format(query) INTO counter;
    END LOOP;
    return results;
end;
$$;

Model classes were created by scaffold command and in the context class I have the following lines (just one for example - all another lines are identical): entity.Property(e => e.Uid).HasDefaultValueSql("configs.random_uid('access_rules')") and in the Model class:

[Column("uid", TypeName = "varchar")]
[MaxLength(30)]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Uid { get; set; }

However, when I try to add, I get the folowing exception message :

value too long for type character varying(30)

Trying to get Uid field value, o got the value:

c2d29867-3d0b-d497-9191-18a9d8ee7830

This value is not in the pattern that my function returns, since the function "translate" all numbers for alpha chars and exclude all "-" occurrences. To "temporary solve" this problem, I wrote the following commands in Controller:

var command = _context.Database.GetDbConnection().CreateCommand();
command.CommandText = "SELECT configs.random_uid('access_rules')";
_context.Database.OpenConnection();

AccessRules.Uid = command.ExecuteScalar().ToString();

I know this is a very dirty "solution", but I could not find any other solution, and the .HasDefaultValueSql() seems to not working in my case.
And before someone ask me about PostgreSQL extensions, yes, I installed "uuid-ossp" and "pgcrypto".

Can someone help me to solve this problem?

And I have another not relevant question.When I add a new Registry, is not easier to omit PK column name and PK column value (or any other columns that have default values) in the generated query, when I have a default value for these columns?

Activity

  1. roji commented on Jun 9, 2018

    @roji
    Member

    Sorry for not answering this sooner and helping out.

    It's probably a very bad idea for you to generate your UUIDs like this - you're aware of uuid-ossp and pgcrypto, so why not use their UUID generation functions?

    In any case, I'm not going to dive into what you're doing in your function... PostgreSQL is telling you that you're trying to put too many characters in there. If this issue is still relevant to you and you need guidance, I first suggest you post on stackoverflow (tag with npgsql and efcore) as this seems to be more of a usage issue.

  2. tontm1981 commented on Jun 11, 2018

    @tontm1981
    Author

    What I am trying to say, it's useless write a function to generate value (uuid or not), because NPGSQL simply ignores it and override my work, executing any other thing, and it's an unexpected behavior. I wrote a function to generate values and defined, in table creation, that I will use this function as default value in a column (and here, it does not matter if it is a UUID or not), and what anyone expects is that what you write will be executed as you write.

    Again, if I define a default value to a column in a table, is not easier simply left this action to DB?? "HasDefaultValueSQL" supposes that NPGSQL will execute exactly what is defined in DB.

    And more, If NPGSQL ignores what I wrote and execute what it wants, how can I trust in a framework that executes anything else than my code?? All the time I spent in this work was wasted to trash.

  3. roji commented on Jun 11, 2018

    @roji
    Member

    @tontm1981 Npgsql and EF Core definitely do not ignore default value generation... (and this doesn't seem to be what your issue above is about). When defining your model, you can use HasDefaultValue() and HasDefaultValueSql() just like the EF Core docs describe, and this will create database columns with the appropriate default clauses. Many people are using this basic feature and it works well.

    Before thinking that the framework is ignoring your code, can you please post a full, clear code sample and explain exactly what's not working the way you expect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions