Supabase database function using foreign keys - supabase

I am using supabase for the first time and I am facing a problem on the signup process.
I use a database function, using the model from the SQL editor quick start. Here is the function I use, triggered on on_auth_user_created :
begin
insert into public.profiles (id, full_name, team)
values (new.id, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'team');
return new;
end;
This is the database structure :
Table teams :
id : int8
name : text
Table profiles :
id : uuid references auth.user.id
team : int8 references public.teams.id
full_name : text
Now, here is the problem, when using only id and full_name on the database function, it work, but when I had team I get AuthApiError: Database error saving new user.
I find nothing on the web and I have no idea why I can't make it work.

Related

Way to get GORM/Hibernate to work with trigger that sets primary key

I have an existing Oracle database that sets the primary key for an insert via a trigger.
TRIGGER SET_schedtemplate_id_template
BEFORE INSERT
ON schedtemplate
FOR EACH ROW
BEGIN
SELECT schedtemplate_id_template_SEQ.NEXTVAL
INTO :NEW.id_template
FROM DUAL;
END;
We have other applications that depend on this approach for this database
I want to be able to map this database in GORM in my domain object
static mapping = {
autoTimestamp true
table 'schedtemplate'
version false
id column: 'id_template', generator: 'sequence', params: [sequence: 'SCHEDTEMPLATE_ID_TEMPLATE_SEQ']
}
The problem with this approach is that GORM increments the sequence to say 12 but then on insert the sequence gets incremented again to 13. This means other objects in the object graph violate foreign key constraints as they are using GORM's 12 instead of the trigger's 13.
It appears the hibernate setting hibernate.jdbc.use_get_generated_keys = true was developed for this purpose.
How do I configure GORM/Grails to use this setting?
The trigger assigned identity column in Hibernate was discussed here hibernate and DB triggers
Now there is a question, how to configure it in GORM.
Try to use the custom identity generator described above like this :
static mapping = {
...
id column: 'id_template', generator: 'jpl.hibernate.util.TriggerAssignedIdentityGenerator'
}

Auto Generated Id Using Fluent NHibernate with Oracle 11g

I am having issues inserting records using Fluent NHibernate. The code is trying to get a number from a sequence that is non-existent for the KEY field.
{"could not insert: [Class Name ][SQL: INSERT INTO Schema.TableName (KEY, ID) VALUES (hibernate_sequence.nextval, ?) returning KEY into :nhIdOutParam]"}
Of course the hibernate_sequence sequence doesn’t exist in the database. If I do an insert using SQL Developer say:
INSERT INTO Schema.TableName (ID) VALUES (90); this works and my primary key (KEY) is auto-generated.
I know you can use a sequence to auto generate this value using GenerateBY.Sequence() but is there a way to insert the record using the SQL statment about using Fluent Nhibernate?
I have my class mapped to the primary key in my class for ex.
Id(x => x.Id, "KEY");
Use this. It will insert all of the fields except for the Id which will let Oracle set it for you.
Id(x => x.Id, "KEY").GeneratedBy.Increment();

Get the latest inserted PK after submitting a linq insert stored procedure

I have a stored procedure that updates a table using linq, eg: (this is just example code by way)
using (DataContext db = new DataContext())
{
d.sp_Insert_Client( textboxName.Text, textBoxSurname.Text);
}
What I would like to know is how to retrieve (if possible) newly generated primary key of the above inserted row, as I need this primary key as a foreign key to complete another insert.
You have to modify your stored procedure to return that value from database and then regenerate your Linq mapping to update that change in your ORM files. After that your sp_Insert_Client method will return an integer.
The other way to do that is to add another parameter into the query and mark it as output one.
To get last inserted I'd inside your SP use SCOPE_IDENTITY: http://msdn.microsoft.com/pl-pl/library/ms190315.aspx
I think you need to retrieve value by using the output parameter that you can check over here : Handling stored procedure output parameters A Scott Gu post which explain that easily
Procedure
For you
create procdeudre nameofprocedure
#id int output
as
begin
insert in your table statement
--retrieve identity value
select #id = scope_identity();
end
Code

Type reference scope

I'm studying databases and am currently working on a object-relational DB project and I've encountered a small problem with the number of possible constraints in an object table. I'm using "Database Systems: The Complete Book" by Hector Garcia-Molina (and other authors) as a reference and there's a general SQL example like this:
CREATE TYPE StarType AS (
name CHAR(30),
address AddressType,
bestMovie REF(MovieType) SCOPE Movies
);
Now, I have a kind of a similar type in my project, as it also uses reference to another type within a type, but the clause for placing a reference there doesn't include SCOPE in Oracle (at least I haven't found it in the docs and it outputs an error). So I have a type like this:
CREATE OR REPLACE TYPE "ApplicationType" AS OBJECT (
"person" REF "PersonType",
"competition" REF "CompetitionType",
"dateApplied" DATE
);
/
...which works. But when I want to constrain the REF columns, I can constrain only one, as so:
CREATE TABLE "Applications" OF "ApplicationType" (
"person" SCOPE IS "People" /* or "competition" SCOPE IS "Competitions" */
)
OBJECT IDENTIFIER IS SYSTEM GENERATED;
Is there any way to give constraints to both REF columns?
This works just fine:
CREATE TABLE Applications OF ApplicationType (
person SCOPE IS People,
competition SCOPE IS Competitions
)
OBJECT IDENTIFIER IS SYSTEM GENERATED;
Maybe you tried creating the table using or instead of , for separating the constraints(as seen in your comment).
It's also easy to test your constraints. Just create these two additional dummy tables:
CREATE TABLE People2 OF PersonType
OBJECT IDENTIFIER IS SYSTEM GENERATED;
CREATE TABLE Competitions2 OF CompetitionType
OBJECT IDENTIFIER IS SYSTEM GENERATED;
Then:
INSERT INTO People VALUES('p1');
INSERT INTO People2 VALUES('p21');
INSERT INTO Competitions VALUES('c1');
INSERT INTO Competitions2 VALUES('c21');
COMMIT;
INSERT INTO Applications
VALUES
(
(SELECT REF(p) FROM People p WHERE person = 'p1'),
(SELECT REF(c) FROM Competitions2 c WHERE competition = 'c21'),
SYSDATE
);
results in an ORA-22889 since the refered value is not in the specified scoped table(which is Competitions, not the dummy Competitions2).
You can test similarly using People2 instead of People.

NHibernate - Composite-Id on relation - Where ID of parent is not same composite

Say I have these table in an Oracle database
Person:
PersonId <- PK ID (int, from sequence)
PersonFirstNameID <-- int
PersonLastNameID <-- int
PersonSecurityID <-- int
PersonDetails
PersonFirstNameID -CompositeKey
PersonLastNameID -CompositeKey
PersonSecurityID -CompositeKey
PersonDetailKey
PersonDetailValue
PersonDetailRisk
Now I want to model the one to many relation from Person to PersonDetails in NHibernate.
How can I do this? I've tried setting up a component representing the composite Id and feeding this into the one to many via the property ref however this generate SQL with duplicate columns and throws the following:
System.ArgumentException: Identifier type mismatch; Found: Expected:
The NHibernate documentation talks only about doing this when the composite Id is the same on both..
Yes... Its not my DB schema, its a legacy DB and access is very limited.
Not quite clear.
For a foreign key relationship to work, the child must reference the primary key of the parent, so the data structure suggests that the primary key of person is FirstNameId/lastNameid/securityid (and hence your best move would be to ignore the personid column).
Is the Person_Id actually the primary key (defined as a PK in the DB), or is the database's version of PERSON primary key actually FirstNameId/lastNameid/securityid ?

Resources