JTable delete row with multi column key - joomla3.0

I'm try'n to delete a record using JTable delete() on a multi-column table. Table is a linked table defined as:
CREATE TABLE IF NOT EXISTS `#__bb_league_members` (
`user_id` int(10) unsigned NOT NULL ,
`league_id` int(10) unsigned NOT NULL ,
`status` TINYINT NULL DEFAULT 0,
....
PRIMARY KEY (`league_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
since I have no single primary key, I can not use .delete($pk) but I have to load the record into a JTable instance.
$data = [];
$data['user_id'] = $uid;
$data['league_id'] = $lid;
$tbl = $this->getTable('LeagueMember');
$tbl->load($data); //a var dump here shows the record is loaded!!
return $tbl->delete();
according to the Joomla 3.6 docu on JTable.delete($pk)
$pk is: "An optional primary key value to delete. If not set the instance property value is used."
So if I omit the $pk the current loaded instance should be deleted. However I get a "Null primary key not allowed." exception from my code above.
How do I delete a record with a multiple-column key?
PS: I know I can use a SQL statement directly, but my table classes are set up to do tracing/logging, and I would like to stick with using them.

After a little bit more digging I found a 2009 forum post here which states that JTable does not support multi-column or compound keys. :(
So for now, I had to extend JTable with my own multi_column_key_delete() and ... update() functions

Related

Why is Entity Framework ignoring existing GUIDs on insert?

I have a table defined in Sql Server with a GUID primary key and a default of newsequentialid():
CREATE TABLE [dbo].[mything](
[id] [uniqueidentifier] NOT NULL,
[foo] [varchar][32] NULL,
CONSTRAINT [PK_mything] PRIMARY KEY CLUSTERED
(
[id] ASC
)
)
GO
ALTER TABLE [dbo].[mything]
ADD CONSTRAINT [DF_mything_id]
DEFAULT (newsequentialid()) FOR [id]
GO
And when I add an entity with the guid primary key already set, it ends up as a record in the database with a new guid primary key.
var anEntity = new mything
{
id = "17870C25-FC04-EB11-80E9-000C29F38B54",
foo = "Some stuff",
}
dbContext.mythings.Add(anEntity);
dbContext.SaveChanges();
EF seems to ignore the guid that was provided in the record and writes the record with a new Guid.
What I would expect is that the record provided had a null guid, it would be populated with a new guid, and if it was not null it would be used unchanged. But that's not what I'm seeing happen.
I'm seeing duplicate records, with different GUIDs, instead of primary key violation exceptions, because the GUIDs I'm providing in my EF entities are being ignored.
Why could this be happening?
Please tell me this isn't by design!
===
OK, this does seem to be by design.
First, this isn't in SQL server. If I try to insert a record with the id field set, it inserts, or fails with a primary key failure if there is already a record with that id. It only creates a new GUID for the id field if the provided id field is null.
But in EF, the value in the id field is ignored, and a new GUID is generated every time.
It was suggested to me that EF was behaving this way so as to follow the same pattern as when using autoincrement keys. And that does seem to be the case.
In SQL Server, if you try to provide a value to an autoincrement key field on an insert, you get an error:
Cannot insert explicit value for identity column in table
But in EF, the value you provide is ignored, and a new value is generated.
So in this respect, Entity Framework is consistent. Consistently wrong, but consistent.
First step I'd look at to narrow this down is to capture a Profiler/Extended Event trace at the database to see exactly what EF is sending to the database.
Your expectation of the database behaviour is correct - so I'd want to understand where it is breaking down first

PostgreSQL Error: Relation already exists - FOREIGN KEY in CREATE TABLE

I am making a table as follows:
CREATE TABLE creator.lists
(
_id bigserial PRIMARY KEY NOT NULL,
account_id bigint NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
display_name text DEFAULT '',
name text DEFAULT '',
extra jsonb,
FOREIGN KEY (account_id)
REFERENCES creator.accounts (_id)
ON DELETE CASCADE
);
but I get this error:
ERROR: relation "account_id_index" already exists
When I run:
CREATE INDEX
account_id_index
ON
creator.lists
(
account_id
);
How do I create an index on the foreign key? I am running v11.1
Just a note, that I've also ran a similar command before for another table:
CREATE INDEX
account_id_index
ON
creator.contacts
(
account_id
);
I don't suppose that index names need to be unique between tables?
Indexes live in the same namespace as tables, views and sequences, so you cannot use the same name twice for any of these objects in one schema.
Either choose a different name, or have PostgreSQL choose one for you:
CREATE INDEX ON creator.lists (account_id);
Okay, it seems like index names need to be unique as removing the naming fixed it:
CREATE INDEX
ON
creator.contacts
(
account_id
);
From the documentation:
name
The name of the index to be created. No schema name can be included here; the index is always created in the same schema as its parent table. If the name is omitted, PostgreSQL chooses a suitable name based on the parent table's name and the indexed column name(s).

How can i add a default null in Oracle Database

Hello everyone i am new to databases and i am trying to make add a foreign key the first time i tried i had the following error
ORA-01735: invalid ALTER TABLE option when i googled it it found something like DEFAULT NULL: But when i use it i get the Following error code RA-00904: : invalid identifier
My alter Table looks like this
alter table car
add constraint priceCar DEFAULT NULL foreign key (note) references priceCode(note) DEFAULT NULL;
I dont know what i am doing wrong this is my first time using databases Oracle
Adding the constraint would just be:
alter table car
add constraint priceCar foreign key (note) references priceCode(note);
assuming the relevant column already exists in both tables and is the same data type, and is a primary or unique key in priceCode.
The default value for a column is a completely unrelated issue, and as this is presumably a number column it will default to null anyway. You can explicitly add a default null clause to the column definition (not the constraint) but you don't need to.
Quick demo with made-up tables:
create table pricecode(note number primary key);
Table PRICECODE created.
create table car (id number, note number);
Table CAR created.
alter table car
add constraint priceCar foreign key (note) references priceCode(note);
Table CAR altered.
insert into car (id) values (1);
1 row inserted.
Then:
select * from car;
ID NOTE
---------- ----------
1
shows that note defaulted to null without you having to explicitly set that up (and also that it didn't complain about it not matching any primary key value).

Auto increment primary key in SQL Server Management Studio 2012

How do I auto increment the primary key in a SQL Server database table? I've had a look through the forum but can't see how to do this.
I've looked at the properties but can't see an option. I saw an answer where you go to the Identity specification property and set it to yes and set the Identity increment to 1, but that section is grayed out and I can't change the no to yes.
There must be a simple way to do this but I can't find it.
Make sure that the Key column's datatype is int and then setting identity manually, as image shows
Or just run this code
-- ID is the name of the [to be] identity column
ALTER TABLE [yourTable] DROP COLUMN ID
ALTER TABLE [yourTable] ADD ID INT IDENTITY(1,1)
the code will run, if ID is not the only column in the table
image reference fifo's
When you're creating the table, you can create an IDENTITY column as follows:
CREATE TABLE (
ID_column INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
...
);
The IDENTITY property will auto-increment the column up from number 1. (Note that the data type of the column has to be an integer.) If you want to add this to an existing column, use an ALTER TABLE command.
Edit:
Tested a bit, and I can't find a way to change the Identity properties via the Column Properties window for various tables. I guess if you want to make a column an identity column, you HAVE to use an ALTER TABLE command.
You have to expand the Identity section to expose increment and seed.
Edit: I assumed that you'd have an integer datatype, not char(10). Which is reasonable I'd say and valid when I posted this answer
Expand your database, expand your table right click on your table and select design from dropdown.
Now go Column properties below of it scroll down and find Identity Specification, expand it and you will find Is Identity make it Yes. Now choose Identity Increment right below of it give the value you want to increment in it.
CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.
In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.
Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will NOT have to specify a value for the "Personid" column (a unique value will be added automatically):
Perhaps I'm missing something but why doesn't this work with the SEQUENCE object? Is this not what you're looking for?
Example:
CREATE SCHEMA blah.
GO
CREATE SEQUENCE blah.blahsequence
START WITH 1
INCREMENT BY 1
NO CYCLE;
CREATE TABLE blah.de_blah_blah
(numbers bigint PRIMARY KEY NOT NULL
......etc
When referencing the squence in say an INSERT command just use:
NEXT VALUE FOR blah.blahsequence
More information and options for SEQUENCE
When you're using Data Type: int you can select the row which you want to get autoincremented and go to the column properties tag. There you can set the identity to 'yes'. The starting value for autoincrement can also be edited there. Hope I could help ;)
I had this issue where I had already created the table and could not change it without dropping the table so what I did was:
(Not sure when they implemented this but had it in SQL 2016)
Right click on the table in the Object Explorer:
Script Table as > DROP And CREATE To > New Query Editor Window
Then do the edit to the script said by Josien; scroll to the bottom where the CREATE TABLE is, find your Primary Key and append IDENTITY(1,1) to the end before the comma. Run script.
The DROP and CREATE script was also helpful for me because of this issue. (Which the generated script handles.)
You can use the keyword IDENTITY as the data type to the column along with PRIMARY KEY constraint when creating the table.
ex:
StudentNumber IDENTITY(1,1) PRIMARY KEY
In here the first '1' means the starting value and the second '1' is the incrementing value.
If the table is already populated it is not possible to change a column to IDENTITY column or convert it to non IDENTITY column. You would need to export all the data out then you can change column type to IDENTITY or vice versa and then import data back.
I know it is painful process but I believe there is no alternative except for using sequence as mentioned in this post.
Be carefull like if you want the ID elements to be contigius or not. As SQLSERVER ID can jump by 1000 .
Examle: before restart ID=11
after restart , you insert new row in the table, then the id will be 1012.
You could do the following: New Table Creation:
-- create new table with Column ID which is Primary Key and Auto Increment --
CREATE TABLE titles(
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, --Primary Key with Auto-Increment --
keyword VARCHAR(260),
status VARCHAR(10),
);
If you Table Already exists and need to make the changes to ID column to be auto-increment and Primary key, then see below:
ALTER TABLE table DROP COLUMN id; // drop the existing ID in the table
ALTER TABLE table ADD id int IDENTITY(1, 1) NOT NULL; // add new column ID with auto-increment
ALTER TABLE table ADD CONSTRAINT PK_ident_test PRIMARY KEY CLUSTERED (id); // make it primary key

DbMetal chokes on repeated foreign key references in SQLite - any ideas?

I've been struggling to get DbMetal to process my SQLite database. I finally isolated the problem. It won't allow a table to have two foreign key references to the same column.
For example, a SQLite database with these two tables will fail:
CREATE TABLE Person
(
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL
);
CREATE TABLE Match
(
Id INTEGER PRIMARY KEY,
WinnerPersonId INTEGER NOT NULL REFERENCES Person(Id),
LoserPersonId INTEGER NOT NULL REFERENCES Person(Id)
);
I get this error:
DbMetal: Sequence contains more than one matching element
If I get rid of the second foreign key reference, no error occurs.
So, this works:
CREATE TABLE Match
(
Id INTEGER PRIMARY KEY,
WinnerPersonId INTEGER NOT NULL REFERENCES Person(Id),
LoserPersonId INTEGER NOT NULL
);
But I really need both "person" columns to reference the Person table.
I submitted a bug report for this, but I could use a workaround in the meantime. Any ideas?
I just had the same problem and created a patch. I've also posted it at your bug report. For others, you can find the patch here: http://pastebin.com/VhNptMqp.

Resources