0

I have a pretty simple code on csharp that should insert into table but with enabled RLS (insert for only authenticated policy), every time I try to insert, I receive violation of RLS, with it disabled, works fine. Maybe I am stupid...

Code:

    [Table("cities")]
class City : BaseModel
{
    [PrimaryKey("id", false)]
    public int Id { get; set; }

    [Column("name")]
    public string Name { get; set; }

    [Column("country_id")]
    public int CountryId { get; set; }
}

.............................................................

        Console.Write("Please enter your email: ");
        email = Console.ReadLine();

        Console.Write("Password: ");
        password = ReadPassword();

        if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("Email and password cannot be empty.");
            return;
        }
    }
    
    try
    {
        var url = ("projectlink");
        var key = ("mykey");

        var options = new SupabaseOptions
        {
            AutoConnectRealtime = true
        };

        var supabase = new Supabase.Client(url, key, options);
        await supabase.InitializeAsync();

        var session = await supabase.Auth.SignIn(email, password);

        //Auth works fine, I checked.

        if (session != null)
        {
            Console.WriteLine("Signed in!");
            string systemGuid = SystemGuid.Value();
            Console.WriteLine("Welcome back " + email + "!");
            Console.WriteLine("Your Hardware Fingerprint: " + systemGuid);
            var model = new City
            {
                Name = "The Shire",
                CountryId = 554
            };

            await supabase.From<City>().Insert(model);

I assume that issue is - it doesn't include something like session or auth token in request, but I haven't found anything related to this in docs :(

Also, don't mark as duplicate, the issue persists only on c#, js and ts works ok!!!

Thanks!

1

1 Answer 1

0

When you use the Supabase C# library to insert a row into a table, it uses a SQL statement called INSERT INTO. This statement has two parts: the INSERT part, which inserts the new row, and the SELECT part, which selects the new row from the table.

The SELECT part is used to get the primary key of the new row. This is necessary because the INSERT part needs to know the primary key in order to insert the row into the table.

If you have Row Level Security (RLS) enabled on your Supabase database, you need to grant the SELECT permission to the table in order to allow users to insert rows into the table.

Here is a summary of the steps involved in inserting a row into a Supabase table with RLS enabled:

Grant the SELECT permission to the table to the users who need to be able to insert rows into the table. Use the Supabase C# library to insert the row into the table. The Supabase C# library will use the SELECT permission to get the primary key of the new row. The Supabase C# library will use the primary key to insert the row into the table.

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.